动态SQL - ','附近的语法不正确

时间:2015-09-15 21:39:35

标签: sql-server dynamic-sql

我有以下存储过程,当我执行此存储过程时,我收到错误

Msg 102,Level 15,State 1,Line 2 ','。

附近的语法不正确

例如:spORGANIZATION_MERGE'1','555,766'

我打印了看起来像这样的SQL,并且在已加星标的行上发生了错误。如果我单独执行语句,我没有收到错误。

我尝试在每个语句的末尾添加“CHAR(13)+ CHAR(10)+ GO”,但这导致“GO错误附近的语法错误。请帮助

UPDATE a_PDF_BUILDER_PDFVERSION SET entity_id = 1 WHERE entity_id IN (      SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) AND entity_dataobject_key = 'Organization' ;
**UPDATE a_WF_PROJECT_ORGANIZATION SET organization_id = 1 WHERE organization_id IN ( SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) ;**
UPDATE ADDRESS SET entity_id = 1 WHERE entity_id IN ( SELECT Data FROM  dbo.BreakStringIntoRows( '555,766', ',')) AND entity_type_id = 2 ;
UPDATE CONTACT_CATEGORY_MEMBER SET entity_id = 1 WHERE entity_id IN ( SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) AND entity_type_id = 2 ;
UPDATE EDUCATION SET organization_id = 1 WHERE organization_id IN ( SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) ;
UPDATE EMAIL SET entity_id = 1 WHERE entity_id IN ( SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) AND entity_type_id = 2 ;
UPDATE EMAIL_LOG_ENTITY_INFO SET entity_id = 1 WHERE entity_id IN ( SELECT Data FROM dbo.BreakStringIntoRows( '555,766', ',')) AND entity_dataobject_key = 'Organization' ;


CREATE PROCEDURE spORGANIZATION_MERGE

@MasterOrgID Varchar(10),
@OtherOrgIDs Varchar(MAX)

AS

DECLARE @ID INT,
@ColumnName VARCHAR(MAX),
@TableName VARCHAR(MAX),
@Count INT,
@Index INT = 1,
@SQL NVARCHAR(MAX)

    SELECT ROW_NUMBER() OVER( ORDER BY t.name) AS 'Row', C.name AS ColName, T.name AS TableName INTO #TempOrganizationTables
    FROM sys.columns C
        JOIN sys.tables T ON C.object_id = T.object_id
    WHERE C.name IN ('Organization_id','entity_id','OrganizationID','primary_organization_id') AND t.name <> 'ORGANIZATION'

    SELECT @Count = COUNT(*) FROM #TempOrganizationTables

    WHILE @Index <= @Count
    BEGIN
        SET @SQL = NULL

        SELECT @ColumnName = ColName, @TableName = TableName FROM #TempOrganizationTables WHERE Row = @Index

        IF @ColumnName = 'entity_id'
        BEGIN

            IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND COLUMN_NAME = 'entity_type_id' )
            BEGIN
                SET @SQL =  N'UPDATE ' + @TableName + ' SET ' + @ColumnName + ' = ' + @MasterOrgID + ' WHERE ' + @ColumnName + ' IN ( SELECT Data FROM dbo.fnSplit_String( ''' + @OtherOrgIDs + ''', '','')) AND entity_type_id = 2 ;' 
            END
            ELSE IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName AND COLUMN_NAME = 'entity_dataobject_key')
            BEGIN
                SET @SQL =  N'UPDATE ' + @TableName + ' SET ' + @ColumnName + ' = ' + @MasterOrgID + ' WHERE ' + @ColumnName + ' IN ( SELECT Data FROM dbo.fnSplit_String( ''' + @OtherOrgIDs + ''', '','')) AND entity_dataobject_key = ''Organization'' ;' 
            END

        END
        ELSE
        BEGIN
            SET @SQL =  N'UPDATE ' + @TableName + ' SET ' + @ColumnName + ' = ' + @MasterOrgID + ' WHERE ' + @ColumnName + ' IN ( SELECT Data FROM dbo.fnSplit_String( ''' + @OtherOrgIDs + ''', '','')) ;' 
        END

        IF @SQL IS NOT NULL
            EXEC sp_executesql @SQL ;       
            --print @SQL

        SET @Index = @Index + 1

    END


GO

UDF看起来像这样

alter FUNCTION [dbo].[fnSplit_String]
(
    @RowData VARCHAR(MAX),
    @SplitOn CHAR(1)
)  
RETURNS @RtnValue table 
(
    --Id int identity(1,1),
    Data VARCHAR(MAX)
) 
AS  
BEGIN 
  Declare @Cnt int
  Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
    Insert Into @RtnValue (data)
    Select 
        Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

    Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
    Set @Cnt = @Cnt + 1
End

Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))

Return
END

GO

0 个答案:

没有答案