MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了sql-server – sql脚本变量的默认值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个脚本文件,例如TEST.sql.我想用另一个脚本来调用它,比如Caller.sql,在sqlcmd模式下使用:r test.sql.这工作正常,但我想在test.sql中使用脚本变量.当我从caller.sql调用test.sql时,我可以设置脚本变量,一切都很好.但是,我想使用脚本值的默认值,以便如果调用者没有设置变量,或者我直接运行test.sql(而不是从caller.sql),那么脚本变量默认为设置值.

我尝试过诸如此类的东西

begin try
 SELEct '$(grip)'
 SELEct 'grip value was found'
end try
begin catch
 SELEct 'grip value was missing'
end catch

但我得到以下消息:
发生了致命的脚本错误.
未定义可变夹点.

我在test.sql中需要什么才能处理调用者传递的“grip”?我正在使用MS sql 2005

解决方法

有一个有限的解决方法(我只在SS2008R2上测试过):

SIMPLE版本 – 如果你愿意没有:错误退出/ sqlcmd.exe -b:

:on error ignore -- Ensures that sqlcmd.exe will not fail when referencing an undefined scripTing variable. Remove this if you want your script to work in SSMS in regular mode,too.
Declare @valueOrDefault as nvarchar(maX)= N'$(value)';    
if @valueOrDefault = N'$' + N'(value)' set @valueOrDefault = N'default value'; -- Test if there is a value and,if not,assign a default; note the splitTing of the reference String to avoid expansion.

-- use @valueOrDefault from Now on

注意:

>由于T-sql变量不能跨批处理工作,因此无法启动另一个批处理(使用GO),因此无法在错误退出时切换到强大的错误处理.
因此,你必须在脚本的其余部分进行自己的错误处理 – 这是非常重要的;见SQL Server – stop or break execution of a SQL script
>如果删除:on on ignore以使脚本在常规模式下在SSMS中工作,请确保在使用sqlcmd.exe调用该脚本时未指定-b选项,因为这将阻止整个如果引用的脚本变量不存在,则运行脚本.
>通过有效地将脚本变量转换为常规T-sql变量,您无法在T-sql期望文字的位置使用该值,例如CREATE DATABASE语句中的数据库名称.
>如果未定义脚本变量,则会向stderr打印以下警告:
‘variablename’脚本变量未定义.

ROBUST版本 – 更麻烦,但支持:在错误退出时,这是可取的:

-- Store the default value in the context info (session-level storage accessible across batches that holds up to 128 bytes).
declare @binDefaultValue varbinary(128)= CAST(N'default value' AS varbinary(128));
set COntexT_INFO @binDefaultValue;
go -- Make the set COntexT_INFO statement take effect.

-- If the scripTing variable has a value,store ITS value in the context info instead.
:on error ignore -- Temporarily ignore errors so that accessing a non-existent scripTing variable doesn't abort the entire script.
    declare @value as nvarchar(maX) = N'$(value)'; -- Try to access the scripTing variable; thanks to :on error ignore this will only give a warning.
    if @value <> N'$' + N'(value)' -- Test if there is a value; note the splitTing of the reference String to avoid expansion.
    begin
    -- We have a scripTing-variable value: Store it in the context info (replacing the default value).
        declare @binValue as varbinary(128) = cast(@value as varbinary(128));
        set COntexT_INFO @binValue;
    end
go -- End batch here,so we can switch BACk to :on error exit (requires a new batch).

:on error exit -- New batch: switch BACk to robust error handling.
-- End the batch here,so that SSMS in *regular* mode - which will fail on the line above - conTinues processing below.
-- Note that when run by sqlcmd.exe the subsequent batches will inherit :on error exit.
go

-- Retrieve the value or default value from the context info...
declare @valueOrDefault as nvarchar(maX) = convert(nvarchar(maX),COntexT_INFO(),0);
-- ... and remove Trailing null characters. ?? Is there an easier way to do this?
declare @pos as int = 0;
while @pos < LEN(@valueOrDefault)
begin
    set @pos=@pos+1
    if UNICODE(subString(@valueOrDefault,@pos,1)) = 0  break;
end
if @pos > 0 set @valueOrDefault = left(@valueOrDefault,@pos - 1);

-- @valueOrDefault Now contains the scripTing-variable value or default value.
print 'Value or default value: [' + @valueOrDefault + ']';

注意:

>以上工作在从sqlcmd.exe调用时和SSMS中以常规模式调用 – 假设您在脚本中不使用其他sqlCMD命令.遗憾的是,sqlCMD模式下的SSMS始终拒绝运行引用不存在的脚本变量的脚本.>需要使用SET COntexT_INFO,因为需要跨批处理边界传递值,这是使用T-sql变量无法完成的.需要多个批次才能切换回强大的错误处理.>上面的代码仅支持单个脚本变量,并且由于使用SET COntexT_INFO,其长度限制为128个字节= 64个Unicode字符;但是,可以想象使用其他解决方法,例如临时表.>通过有效地将脚本变量转换为常规T-sql变量,例如CREATE DATABASE语句中的数据库名称.>如果未定义脚本变量,则会向stderr打印以下警告:‘variablename’脚本变量未定义.

大佬总结

以上是大佬教程为你收集整理的sql-server – sql脚本变量的默认值全部内容,希望文章能够帮你解决sql-server – sql脚本变量的默认值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:那么