Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Stored Procedure
Message
Information générale
Forum:
ASP.NET
Catégorie:
Bases de données
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Database:
MS SQL Server
Divers
Thread ID:
01433610
Message ID:
01433643
Vues:
32
>>>>>>I didn't look too close yet, but looks like there is an extra comma at the end of cmd line - not sure how did it get there.
>>>>>
>>>>>Right, I took out the extra commas, and it saved successfully, but when I tried to run it, I get this:
>>>>>
>>>>>Must declare the scalar variable "@StartDate".
>>>>
>>>>Getting this error right now - give me a sec.
>>>
>>>Of course!!!
>>>
>>>Here is the correct version
>>>
>>>
>>>-- Test query
>>>alter PROCEDURE [dbo].[GetWellData] 
>>>	-- Add the parameters for the stored procedure here	
>>>	@Tablename sysname,	 
>>>	@StartDate datetime,
>>>	@EndDate datetime
>>>AS
>>>BEGIN
>>>	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
>>>	SET NOCOUNT ON;
>>>	SET @TableName = RTRIM(@TableName)
>>>
>>>	Declare @cmd AS NVARCHAR(max)
>>>   DECLARE @ParmDefinition NVARCHAR(500)
>>>   SET @ParmDefinition = N'@StartDate datetime ,@EndDate datetime'
>>>
>>>	SET @cmd = N'Select * from ' + QUOTENAME(@TableName) + ' where BidDateTime between @StartDate and @EndDate'
>>>    
>>>	exec sp_executesql @cmd, @ParmDefinition,
>>>            @StartDate = @StartDate,
>>>            @EndDate  = @EndDate
>>>END
>>>go
>>>
>>>execute GetWellData 'Bid', '20081001','20091101'
>> That did it, Thanks Naomi.
>
>If you use BETWEEN to check a date range, you must be sure that there cannot be any null values in either of those date columns. If either date field is NULL, the row will not be selected.

Excellent point. I was in an interview the other day, getting grilled by three guys, mostly about SQL, and one of the questions was what is the maximum number of rows returned by this query? --

SELECT DISTINCT MONTH(whateverDate) FROM whateverTable

I said 12. He smiled and said not quite. I should have grokked it right away but didn't. You forgot null, he said. Palm to forehead time.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform