gpt4 book ai didi

sql - 将整个sql脚本插入变量中

转载 作者:行者123 更新时间:2023-12-02 15:47:35 25 4
gpt4 key购买 nike

我在变量中插入 sql 脚本时遇到问题。我有这段代码

   declare @result_var varchar(max);
SET @result_var='';
DECLARE @cursor CURSOR,
@name VARCHAR(100)
SET @cursor = CURSOR
FOR SELECT [NAME]
FROM [iflvs08].mds.mdm.lv_budget_employee
OPEN @cursor
FETCH next FROM @cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
IF (RIGHT(@Name, 1) = ' ')
begin
IF( Len(@result_var) > 1 )
SET @result_var=@result_var + ','+ @name
ELSE
SET @result_var=@name
END
FETCH next FROM @cursor INTO @name
END

我想将其插入变量,但是当尝试像这样插入它时

set @sql='code here'

这个 ' 符号会让人感到困惑,不要插入它我总是收到这样的错误消息 102,第 15 级,状态 1,第 13 行' 附近的语法不正确 开始 IF( Len(@result_var) > 1 ) SET @result_var=@result_var + '.

最佳答案

基本上,您必须将所有 ' 符号替换为 ''

set @sql = '

declare @result_var varchar(max);
SET @result_var='''';
DECLARE @cursor CURSOR,
@name VARCHAR(100)
SET @cursor = CURSOR
FOR SELECT [NAME]
FROM [iflvs08].mds.mdm.lv_budget_employee
OPEN @cursor
FETCH next FROM @cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
IF (RIGHT(@Name, 1) = '' '')
begin
IF( Len(@result_var) > 1 )
SET @result_var=@result_var + '',''+ @name
ELSE
SET @result_var=@name
END
FETCH next FROM @cursor INTO @name
END'

关于sql - 将整个sql脚本插入变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29302807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com