有人告诉我创建这个存储过程有什么问题。
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'.
答案 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
)。