希望有人能幫忙。我對SQL /編程還很陌生,所以這可能是一個很容易回答的問題。在SQL中聲明變量
對於下面的腳本,我收到一個錯誤,它抱怨3個變量@startdate,@ enddate和@ result1rowcount沒有被聲明。我知道我可以將這些聲明進一步向下移動,它會起作用。但我想理解這個理論爲什麼不能如下所述,因爲我已經在一開始就宣佈了這些變量。這些聲明必須在某個時候丟失。我不確定何時?如果有人能夠解釋爲什麼它會返回錯誤,那會很好。我不想就如何重寫它提出建議。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
Declare @count int
Declare @r int
Declare @start datetime
Declare @end datetime
Declare @startdate datetime
Declare @enddate datetime
Declare @Result1RowCount int
Set @start = DATEADD(d,0,DATEDIFF(d,0,getdate()-1))
set @end = DATEADD(d,0,DATEDIFF(d,0,getdate()))
Create table #tempwill
(feed int,
returnid int,
ProcessStartTime datetime,
ProcessEndTime datetime,
importcount int,
Selectedstartdate datetime,
Selectedenddate datetime)
Insert into #tempwill
Select feed, returnid, processstarttime, processendtime, importcount, selectedstartdate, selectedenddate
from Will_Database.dbo.Import
where selectedstartdate = @start
and selectedenddate = @end
SET @COUNT = (select count(*) from #tempwill where importcount ='0')
IF @Count > 0
Begin
exec @r = Bill_Database.dbo.op_Mail_2005 @profile_name = 'Testserver',
@MailTo = '[email protected]l.com',
@MailBCC = '',
@importance = 'HIGH',
@subject = 'Warning - Numbers are not as expected',
@body = 'Please investigate as todays numbers are not what we expected '
End
GO
**Set @startdate = DATEADD(d,0,DATEDIFF(d,0,getdate()-2))
set @enddate = DATEADD(d,0,DATEDIFF(d,0,getdate()-1))**
CREATE TABLE #Results1 (ID int, Ref int, Code int, Explanation varchar(200), Timeof datetime)
INSERT #Results1 (ID, Ref, Code, Explanation, Timeof)
SELECT A.ID, B.Ref, A.Code, A.Explanation, A.Timeof
FROM Testserver.Will_database.dbo.codetime A
INNER JOIN Testserver.Bill_database.dbo.Coderegister B ON A.Code= B.Code AND A.Ref = B.Ref
WHERE A.Timeof >= @StartDate
AND A.Timeof < @EndDate
**SELECT @Result1RowCount = @@RowCount**
**insert into Daily_Check values
(1, 1, @Result1RowCount, @startdate, @enddate)**
非常感謝。現在完美。 –
「GO」不是SQL命令。這是對處理器(大多數情況下是SQL Server Management Studio,但也包括OSQL.exe和ISQL.exe等實用程序)的命令,用於將長腳本分隔爲「批處理」。 SQL Server本身永遠不會看到一個'GO'命令,如果它發生了,它不知道該怎麼做。 :-) – pmbAustin