1

Here is my stored procedure :

CREATE PROCEDURE uspUpdateDataInBapMidTable (
   @companyName varchar(50)
)
AS
BEGIN
    SET @companyName = 'nürnberger'

    DECLARE @tableNameMid varchar(50) , @tableNameIn varchar(50) , @queryToCreateTable nvarchar (500) , @queryToAddColumn nvarchar(500)
    DECLARE @queryToUpdateBapMid nvarchar(500)
    SET @tableNameMid = 'BAP_'+@companyName+'_MID'
    SET @tableNameIn = 'BAP_'+@companyName+'_IN'
    DECLARE @COGI varchar (50)
    SET @COGI = ''

SET NOCOUNT ON
    DECLARE @collectionOfGesellschaft_id nvarchar(100)
    DECLARE myCursor cursor FOR
    SELECT distinct gesellschaft_id from CRM_Wifo_GmbH.dbo.vertrag_168 where gesellschaft like '%nürnberger%'

    open myCursor
    DECLARE @collectionOfGesellschaft_id1 nvarchar(100)

    set @collectionOfGesellschaft_id  = '('
    fetch next from myCursor into @collectionOfGesellschaft_id1
    while @@fetch_status = 0
    begin
    set @collectionOfGesellschaft_id = @collectionOfGesellschaft_id + @collectionOfGesellschaft_id1 + ' ,'

    fetch next from myCursor into @collectionOfGesellschaft_id1
    END
    set @collectionOfGesellschaft_id = SUBSTRING(@collectionOfGesellschaft_id,1,LEN(@collectionOfGesellschaft_id)-1) + ')'
    SET @COGI = @collectionOfGesellschaft_id
    CLOSE myCursor
    DEALLOCATE myCursor                         
    go

SET @queryToUpdateBapMid = N'select distinct b.*,v.vertrag_id,v.VersicherungsscheinNummer 
from CRM_Wifo_GmbH.dbo.vertrag_168 v,temp_universa b where v.VersicherungsscheinNummer like ''%'' + b.VSNR + ''%''  and v.gesellschaft_id in' + @COGI
EXEC(@queryToUpdateBapMid)

END

@companyName is set for my testing purpose. when I execute the cursor only, it runs fine. but in the stored procedure it shows followingerror :

Msg 102, Level 15, State 1, Procedure uspUpdateDataInBapMidTable, Line 33
Incorrect syntax near 'myCursor'.

Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@COGI".

Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@queryToUpdateBapMid".

I have declare these two variable in the starting of the SP, but they are showing this error. and what is the problem with the line

 DEALLCOATE myCursor
4

1 回答 1

1

问题与go该行之后的单词有关DEALLOCATE myCursor

GO不是一般 SQL 或特定 Transact-SQL 的一部分。它是 SQL Server Management Studio 等工具识别的关键字或sqlcmd作为批处理分隔符。遇到GO时,您的脚本将在该点拆分,并且前面GO的部分与其后面的部分分开执行。

您的go特定脚本中的 过早拆分它。它基本上使最初的BEGIN未关闭,这实际上就是错误的含义,尽管授予该消息可能看起来有些混乱。

无论如何,这是你的问题,所以,只需删除go.

于 2013-01-27T22:45:57.037 回答