使用输入参数创建过程时出现语法错误
Syntax error when using input parameter to create procedure
我遇到了这个错误
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 33, column: 15: Incorrect syntax near '@sas_token'
以下是我遵循的指南供参考:
我尝试了注释部分,因为我认为它无法识别引号,但它仍然不起作用
CREATE PROCEDURE dbo.bulk_load
@sas_token VARCHAR(500)
AS
BEGIN
--DECLARE @sas varchar(500)
--SET @sas = ''''+ @sas_token + ''''
IF OBJECT_ID('dbo.ITEM_INFORMATION') IS NOT NULL
DROP TABLE dbo.ITEM_INFORMATION;
CREATE TABLE dbo.ITEM_INFORMATION
(
model VARCHAR(20),
col1 VARCHAR(15),
col2 VARCHAR(3),
col3 VARCHAR(255)
);
COPY INTO dbo.ITEM_INFORMATION
FROM 'https://server.blob.core.windows.net/container/subfolder/file.csv'
WITH (FILE_TYPE = 'CSV',
FIRSTROW = 2,
CREDENTIAL = (
IDENTITY = 'Shared Access Signature',
SECRET = @sas_token --@sas also tried '''' + @sas_token + ''''
)
);
END
此过程将从 Python 调用,如下所示:
def update_database(sas_token):
#Runs the stored procedure to load the data from Azure storage to table.
#Uses the password stored in user environment variable.
server = 'server.database.windows.net'
database = 'database'
username = 'username'
if 'server_password' not in os.environ:
print("Missing environment variable 'server_password' containing database password.")
password = os.getenv('server_password')
driver= '{ODBC Driver 17 for SQL Server}'
sql = f'EXEC dbo.bulk_load, {sas_token};'
connection = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
connection.autocommit = True
connection.execute(sql)
connection.close()
这应该有效。我已经动态创建了 Copy 命令并将 SAS 令牌传递给它。一旦我创建了复制命令,我就执行命令。打印语句,我用它作为测试。
HTH
CREATE PROCEDURE dbo.bulk_load
@sas_token VARCHAR(500)
AS
BEGIN
--DECLARE @sas varchar(500)
--SET @sas = ''''+ @sas_token + ''''
declare @query varchar(max)
IF OBJECT_ID('dbo.ITEM_INFORMATION') IS NOT NULL
DROP TABLE dbo.ITEM_INFORMATION;
CREATE TABLE dbo.ITEM_INFORMATION
(
SSN VARCHAR(20),
Name VARCHAR(15)
);
SELECT @query ='
COPY INTO dbo.ITEM_INFORMATION
FROM ''https://server.blob.core.windows.net/container/subfolder/file.csv''
WITH (FILE_TYPE = ''CSV'',
FIELDTERMINATOR = ''',''',
FIRSTROW = 2,
ROWTERMINATOR = ''\n'',
CREDENTIAL = (
IDENTITY = ''Shared Access Signature'',
SECRET = '''+@sas_token+'''
)
);'
print @query
exec(@query)
END
我遇到了这个错误
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 33, column: 15: Incorrect syntax near '@sas_token'
以下是我遵循的指南供参考:
我尝试了注释部分,因为我认为它无法识别引号,但它仍然不起作用
CREATE PROCEDURE dbo.bulk_load
@sas_token VARCHAR(500)
AS
BEGIN
--DECLARE @sas varchar(500)
--SET @sas = ''''+ @sas_token + ''''
IF OBJECT_ID('dbo.ITEM_INFORMATION') IS NOT NULL
DROP TABLE dbo.ITEM_INFORMATION;
CREATE TABLE dbo.ITEM_INFORMATION
(
model VARCHAR(20),
col1 VARCHAR(15),
col2 VARCHAR(3),
col3 VARCHAR(255)
);
COPY INTO dbo.ITEM_INFORMATION
FROM 'https://server.blob.core.windows.net/container/subfolder/file.csv'
WITH (FILE_TYPE = 'CSV',
FIRSTROW = 2,
CREDENTIAL = (
IDENTITY = 'Shared Access Signature',
SECRET = @sas_token --@sas also tried '''' + @sas_token + ''''
)
);
END
此过程将从 Python 调用,如下所示:
def update_database(sas_token):
#Runs the stored procedure to load the data from Azure storage to table.
#Uses the password stored in user environment variable.
server = 'server.database.windows.net'
database = 'database'
username = 'username'
if 'server_password' not in os.environ:
print("Missing environment variable 'server_password' containing database password.")
password = os.getenv('server_password')
driver= '{ODBC Driver 17 for SQL Server}'
sql = f'EXEC dbo.bulk_load, {sas_token};'
connection = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
connection.autocommit = True
connection.execute(sql)
connection.close()
这应该有效。我已经动态创建了 Copy 命令并将 SAS 令牌传递给它。一旦我创建了复制命令,我就执行命令。打印语句,我用它作为测试。 HTH
CREATE PROCEDURE dbo.bulk_load
@sas_token VARCHAR(500)
AS
BEGIN
--DECLARE @sas varchar(500)
--SET @sas = ''''+ @sas_token + ''''
declare @query varchar(max)
IF OBJECT_ID('dbo.ITEM_INFORMATION') IS NOT NULL
DROP TABLE dbo.ITEM_INFORMATION;
CREATE TABLE dbo.ITEM_INFORMATION
(
SSN VARCHAR(20),
Name VARCHAR(15)
);
SELECT @query ='
COPY INTO dbo.ITEM_INFORMATION
FROM ''https://server.blob.core.windows.net/container/subfolder/file.csv''
WITH (FILE_TYPE = ''CSV'',
FIELDTERMINATOR = ''',''',
FIRSTROW = 2,
ROWTERMINATOR = ''\n'',
CREDENTIAL = (
IDENTITY = ''Shared Access Signature'',
SECRET = '''+@sas_token+'''
)
);'
print @query
exec(@query)
END