创建存储过程时出错

时间:2010-11-25 06:59:11

标签: sql-server stored-procedures

有人告诉我创建这个存储过程有什么问题。

CREATE PROC ImportData
AS
BEGIN

    DECLARE @DatabasePath VARCHAR(MAX)
    SET @DatabasePath = 'E:\ABC.xls'

    DECLARE @sql      nvarchar(MAX)
    SET @sql = '
    INSERT INTO [dbo].[Table_1]
    SELECT  *
    FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
            ''Excel 8.0;Database=' + @DatabasePath + ',
            ''SELECT * FROM [Sheet1$]'') AS xlsTable'

    EXEC sp_executesql @sql
    GO

END

ERROR:-
Incorrect syntax near '@sql'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'END'.

2 个答案:

答案 0 :(得分:2)

从存储过程中删除GO

这样的东西
CREATE PROC ImportData 
AS 
BEGIN 

    DECLARE @DatabasePath VARCHAR(MAX) 
    SET @DatabasePath = 'E:\ABC.xls' 

    DECLARE @sql      nvarchar(MAX) 
    SET @sql = ' 
    INSERT INTO [dbo].[Table_1] 
    SELECT  * 
    FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', 
            ''Excel 8.0;Database=' + @DatabasePath + ', 
            ''SELECT * FROM [Sheet1$]'') AS xlsTable' 

    EXEC sp_executesql @sql 

END

答案 1 :(得分:1)

您不能在存储过程的正文中使用批处理终结符(GO)。