TSQL从脚本创建/更改存储过程

时间:2014-08-11 09:56:16

标签: sql sql-server-2008 tsql stored-procedures

我编写了一个存储过程,我需要将其添加到SQL脚本中以进行产品更新。

我尝试将其转换为使用sp_executesql但是当我尝试运行它时,我会在引号周围出错。在这种情况下,我不确定问题是什么。知道我错过了什么吗?

原始存储过程:

CREATE PROCEDURE spPortalAttachmentLookupDatabases
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #tmpDatabases
    (
        DatabaseName nvarchar(255)
    )

    EXEC sp_Msforeachdb 'use [?];INSERT INTO #tmpDatabases 
                                SELECT ''?'' AS DatabaseName
                                    FROM sys.columns c
                                        inner join sys.objects  o on c.object_id=o.object_id
                                    WHERE ''?'' NOT IN (''master'',''msdb'',''tempdb'',''model'',''test'',''ReportServer'') AND o.Name = ''AI00'' AND c.Name = ''LookupGuid''
                                    ORDER BY o.name,c.column_id'
    SELECT * FROM #tmpDatabases

    DROP TABLE #tmpDatabases
END
GO

DROP PROCEDURE spPortalAttachmentLookupDatabases

脚本:

DECLARE @SQLCmd as nvarchar(max)
/****** Object:  View [dbo].[spPortalAttachmentLookupDatabases]    Script Date: 14/07/2014 09:16:16 ******/
IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'spPortalAttachmentLookupDatabases') AND type IN ( N'P', N'PC' ) ) 
    SET @SQLCmd = '
CREATE PROCEDURE spPortalAttachmentLookupDatabases
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #tmpDatabases
    (
        DatabaseName nvarchar(255)
    )

    EXEC sp_Msforeachdb ''''INSERT INTO #tmpDatabases 
                                SELECT ''?'' AS DatabaseName
                                    FROM sys.columns c
                                        inner join sys.objects  o on c.object_id=o.object_id
                                    WHERE ''?'' NOT IN (''master'',''msdb'',''tempdb'',''model'',''test'',''ReportServer'') AND o.Name = ''AI00'' AND c.Name = ''LookupGuid''
                                    ORDER BY o.name,c.column_id''''
    SELECT * FROM #tmpDatabases

    DROP TABLE #tmpDatabases'
ELSE
    SET @SQLCmd = '
ALTER PROCEDURE spPortalAttachmentLookupDatabases
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #tmpDatabases
    (
        DatabaseName nvarchar(255)
    )

    EXEC sp_Msforeachdb ''''INSERT INTO #tmpDatabases 
                                SELECT ''?'' AS DatabaseName
                                    FROM sys.columns c
                                        inner join sys.objects  o on c.object_id=o.object_id
                                    WHERE ''?'' NOT IN (''master'',''msdb'',''tempdb'',''model'',''test'',''ReportServer'') AND o.Name = ''AI00'' AND c.Name = ''LookupGuid''
                                    ORDER BY o.name,c.column_id''''
    SELECT * FROM #tmpDatabases

    DROP TABLE #tmpDatabases'

EXEC sp_executesql @SQLCmd
GO

错误

  

Msg 102,Level 15,State 1,Procedure spPortalAttachmentLookupDatabases,Line 130
  ''。

附近的语法不正确      

消息102,级别15,状态1,过程spPortalAttachmentLookupDatabases,第133行
  '#tmpDatabases'附近的语法不正确。

1 个答案:

答案 0 :(得分:0)

出现语法错误。立即尝试:

DECLARE @SQLCmd as nvarchar(max)

/****** Object:  View [dbo].[spPortalAttachmentLookupDatabases]    Script Date: 14/07/2014 09:16:16 ******/

IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'spPortalAttachmentLookupDatabases') AND type IN ( N'P', N'PC' ) )

    SET @SQLCmd = '

CREATE PROCEDURE spPortalAttachmentLookupDatabases
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #tmpDatabases
    (
        DatabaseName nvarchar(255)
    )

    EXEC sp_Msforeachdb ''INSERT INTO #tmpDatabases
                                SELECT  ''''?'''' AS DatabaseName
                                    FROM sys.columns c
                                        inner join sys.objects  o on c.object_id=o.object_id
                                    WHERE ''''?'''' NOT IN (''''master'''',''''msdb'''',''''tempdb'''',''''model'''',''''test'''',''''ReportServer'''') 
                                    AND o.Name = ''''AI00'''' AND c.Name = ''''LookupGuid''''
                                    ORDER BY o.name,c.column_id''
    SELECT * FROM #tmpDatabases
    DROP TABLE #tmpDatabases

    END'

ELSE
    SET @SQLCmd = '

ALTER PROCEDURE spPortalAttachmentLookupDatabases
AS
BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #tmpDatabases
    (
       DatabaseName nvarchar(255)
    )

    EXEC sp_Msforeachdb ''INSERT INTO #tmpDatabases
                                SELECT ''''?'''' AS DatabaseName
                                    FROM sys.columns c
                                        inner join sys.objects  o on c.object_id=o.object_id
                                    WHERE ''''?'''' NOT IN (''''master'''',''''msdb'''',''''tempdb'''',''''model'''',''''test'''',''''ReportServer'''') 
                                    AND o.Name = ''''AI00'''' AND c.Name = ''''LookupGuid''''
                                    ORDER BY o.name,c.column_id''
    SELECT * FROM #tmpDatabases
    DROP TABLE #tmpDatabases

    END'
EXEC sp_executesql @SQLCmd
GO