DEH*_*DEH 8 sql-server sql-server-2005 sqlcmd
我有一个脚本文件,例如test.sql.我想用sqlcmd模式中的另一个脚本调用caller.sql来调用它: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
Run Code Online (Sandbox Code Playgroud)
但我得到以下消息:发生了致命的脚本错误.未定义可变夹点.
我在test.sql中需要什么才能处理调用者传递的"grip"?我正在使用MS SQL 2005
有一个有限的解决方法(我只在 SS2008R2 上测试过):
:on error exit/生活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
Run Code Online (Sandbox Code Playgroud)
笔记:
:on error exit. 因此,您必须在脚本的其余部分进行自己的错误处理 - 这很重要;请参阅SQL Server - 停止或中断 SQL 脚本的执行:on error ignore为了使脚本在常规模式下在 SSMS 中工作而删除,请确保在使用 sqlcmd.exe 调用该脚本时不要指定 -b 选项,因为如果引用的脚本变量不存在。:on error exit,这是可取的:-- 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 + ']';
Run Code Online (Sandbox Code Playgroud)
笔记:
SET CONTEXT_INFO必须使用,因为值需要跨批处理边界传递,而 T-SQL 变量无法做到这一点。需要多批次才能切换回稳健的错误处理。SET CONTEXT_INFO,其长度限制为 128 个字节 = 64 个 Unicode 字符;不过,也可以使用其他解决方法,例如临时表。CREATE DATABASE语句中的数据库名称。也许这三个选项之一:
v:SETVAR本章后面描述的命令SQLCMD。
使用v选项
在您的脚本中:SELECT $(foo) FROM $(bar)
用法:C:>SQLCMD i c:\someScript.sql -v foo="CustomerName" bar="Customer"
使用setvar
:setvar foo CustomerName
:setvar bar Customer
Run Code Online (Sandbox Code Playgroud)
来自
http://www.sqlcmd.org/sqlcmd-scripting-variables/
| 归档时间: |
|
| 查看次数: |
7796 次 |
| 最近记录: |