模仿sp_executesql,制作一个自己的executesql脚本

参照sp_executesql 的使用语法格式是:
sp_executesql [ @stmt = ] stmt

[ 

    {, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' } 

     {, [ @param1 = ] 'value1' [ ,...n ] }

]
参考其中的一些参数的,返回的方法,这里制作一个 MyExecuteSql的存储过程,功能与sp_executesql相似。 这里提供可以输入9个可变参数,当然可以根据自己的爱好增加更多的参数。 这里免不了bug,最大的难点就是数据类型转换,有些数据类型,会转换不一致,有时间再研究研究。呵呵
Use  Test; Go If   Object_id ( ' [MyExecuteSql] ' , ' P ' Is   Not   null      Drop   Proc   [ MyExecuteSql ] Go Create   Proc   [ dbo ] . [ MyExecuteSql ] (      @stmt   nvarchar ( Max ),      @params   nvarchar ( Max ) = null ,      @param1   nvarchar ( Max ) = null  output,      @param2   nvarchar ( Max ) = null  output,      @param3   nvarchar ( Max ) = null  output,      @param4   nvarchar ( Max ) = null  output,      @param5   nvarchar ( Max ) = null  output,      @param6   nvarchar ( Max ) = null  output,      @param7   nvarchar ( Max ) = null  output,      @param8   nvarchar ( Max ) = null  output,      @param9   nvarchar ( Max ) = null  output ) As      Set  Nocount  On Begin  Try      Declare            @xml  xml,           @tmp1   nvarchar ( 1024 ),          @tmp2   nvarchar ( 1024 ),          @Define   nvarchar ( Max ),          @Set   nvarchar ( Max ),          @Update   nvarchar ( Max )      If   Object_id ( ' tempdb..# ' Is   Not   null          Drop   Table  #      Create   Table  #     (    id  smallint   Identity ( 1 , 1 Not   null ,         param  nvarchar ( 1024 ),         datatype  nvarchar ( 1024 ),         flag  char ( 1 ),         value  nvarchar ( max ),         IsOutPut  bit     )      /*     筛分 @params  */      Set   @xml = Convert (xml, Stuff ( Replace ( @params + ' , ' , ' @ ' , ' </a><a>@ ' ), 1 , 4 , '' ) + ' </a> ' )      Insert   Into  # (datatype)          Select  t.m.value( ' . ' , ' nvarchar(1024) ' )          From   @xml .nodes( ' /a ' ) t(m)           Update  #           Set      @tmp1 =Left (datatype, CharIndex ( '   ' ,datatype) - 1 ),             param = @tmp1 ,             IsOutPut = Case   When   Charindex ( '  out ' ,datatype) > 0   Then   1   Else   0   End  ,              @tmp2 = Reverse ( Stuff ( Reverse ( Replace ( Replace ( Replace ( Replace ( Replace (datatype, @tmp1 , '' ), '  output ' , '' ), '  out ' , '' ), ' input ' , '' ), '   ' , '' )), 1 , 1 , '' )),             datatype = @tmp2 ,             flag = Case   When                        CharIndex ( ' tinyint ' , @tmp2 ) > 0   Or                      CharIndex ( ' smallint ' , @tmp2 ) > 0   Or                      CharIndex ( ' int ' , @tmp2 ) > 0   Or                      CharIndex ( ' real ' , @tmp2 ) > 0   Or                      CharIndex ( ' money ' , @tmp2 ) > 0   Or                      CharIndex ( ' float ' , @tmp2 ) > 0   Or                      CharIndex ( ' bit ' , @tmp2 ) > 0   Or                      CharIndex ( ' decimal ' , @tmp2 ) > 0   Or                      CharIndex ( ' numeric ' , @tmp2 ) > 0   Or                      CharIndex ( ' smallmoney ' , @tmp2 ) > 0   Or                      CharIndex ( ' bigint ' , @tmp2 ) > 0   Or                      CharIndex ( ' varbinary ' , @tmp2 ) > 0   Or                      CharIndex ( ' binary ' , @tmp2 ) > 0   Or                      CharIndex ( ' timestamp ' , @tmp2 ) > 0                  Then   ''   Else   ''''   End                           /* 构造执行语句 */      Select            @Define = Isnull ( @Define + ' , ' , ' Declare  ' ) + param + '   ' + datatype,          @Set = Isnull ( @Set + ' , ' , ' Select  ' ) + param + ' =Convert( ' + datatype + ' , ' +              Case  id                   When   1   Then   Isnull (flag + @param1 + flag, ' null ' )                  When   2   Then   Isnull (flag + @param2 + flag, ' null ' )                  When   3   Then   Isnull (flag + @param3 + flag, ' null ' )                  When   4   Then   Isnull (flag + @param4 + flag, ' null ' )                  When   5   Then   Isnull (flag + @param5 + flag, ' null ' )                  When   6   Then   Isnull (flag + @param6 + flag, ' null ' )                  When   7   Then   Isnull (flag + @param7 + flag, ' null ' )                  When   8   Then   Isnull (flag + @param8 + flag, ' null ' )                  When   9   Then   Isnull (flag + @param9 + flag, ' null ' )              End + ' ) ' ,          @Update = Isnull ( @Update + '  When  ' , ' Update # Set value=Case id When  ' ) + Rtrim (id) + '  Then Convert(nvarchar(max), ' + param + ' ) '      From  #      Set   @stmt = @Define + char ( 13 ) + char ( 10 ) + @Set + char ( 13 ) + char ( 10 ) + ' Set Nocount Off ' + char ( 13 ) + char ( 10 ) + @stmt + char ( 13 ) + char ( 10 ) + ' Set Nocount On  ' + char ( 13 ) + char ( 10 ) + @Update + '  End '      Exec ( @stmt )      /* 输出参数 */      Select   @param1 = value  From  #  Where  id = 1   And  IsOutPut = 1      Select   @param2 = value  From  #  Where  id = 2   And  IsOutPut = 1      Select   @param3 = value  From  #  Where  id = 3   And  IsOutPut = 1      Select   @param4 = value  From  #  Where  id = 4   And  IsOutPut = 1      Select   @param5 = value  From  #  Where  id = 5   And  IsOutPut = 1      Select   @param6 = value  From  #  Where  id = 6   And  IsOutPut = 1      Select   @param7 = value  From  #  Where  id = 7   And  IsOutPut = 1      Select   @param8 = value  From  #  Where  id = 8   And  IsOutPut = 1     Select   @param9 = value  From  #  Where  id =9 And  IsOutPut = 1 End  Try Begin  Catch      Declare   @ErrMsg   nvarchar ( 1024 )      Set   @ErrMsg = Error_message()      Raiserror   50001   @ErrMsg End  Catch          
测试:
Declare   @x  numeric( 12 , 3 ) Set   @x = 56.31 Print   ' 结果: ' Print   ' 开始 @x= ' + Rtrim ( @x ) + char ( 13 ) + char ( 10 ) + char ( 13 ) + char ( 10 ) + ' 调用[MyExecuteSql]: ' exec   [ MyExecuteSql ] ' Select @2=@2*5;Select @1,@2,@3 ' , ' @1 nvarchar(20   )  input  ,@2 numeric(12,  3) output , @3   int  ' , ' sdf ' , @x  output, 34 Print   ' 输出 @x= ' + Rtrim ( @x )
/* 结果: 开始 @x=56.310 调用[MyExecuteSql]:                                                              -------------------- -------------------------- sdf                  281.550                                 34 (1 行受影响) 输出 @x=281.550 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SQL Server中,可以使用Transact-SQL来编写脚本同时备份多个数据库。下面是一个示例脚本: ``` DECLARE @dbName VARCHAR(100) -- 存储数据库名 DECLARE @backupPath VARCHAR(100) -- 存储备份路径 DECLARE @backupFile VARCHAR(100) -- 存储备份文件名 DECLARE @sqlScript NVARCHAR(500) -- 存储备份脚本 SET @backupPath = 'C:\Backup\' -- 指定备份路径 SELECT name INTO #tempDBs FROM sys.databases -- 创建临时表存储数据库名 WHILE EXISTS (SELECT TOP 1 * FROM #tempDBs) BEGIN SELECT TOP 1 @dbName = name FROM #tempDBs SET @backupFile = @backupPath + @dbName + '_' + REPLACE(CONVERT(VARCHAR,GETDATE(),120), ':', '_') + '.bak' -- 指定备份文件名 SET @sqlScript = 'BACKUP DATABASE ' + @dbName + ' TO DISK = ''' + @backupFile + ''' WITH FORMAT' -- 构建备份脚本 EXEC sp_executesql @sqlScript -- 执行备份脚本 DELETE FROM #tempDBs WHERE name = @dbName -- 从临时表中删除已备份的数据库 END DROP TABLE #tempDBs -- 删除临时表 ``` 上述脚本中,首先创建了一个临时表用于存储所有数据库的名称。然后使用循环语句,依次获取临时表中的数据库名,并构建备份路径和文件名。接着使用`BACKUP DATABASE`语句执行备份操作,并通过`sp_executesql`存储过程执行动态生成的备份脚本。最后,删除已备份的数据库名记录,并清除临时表。 执行该脚本可以同时备份多个数据库,将备份文件保存在指定的备份路径中。注意,该脚本只是一个示例,实际使用时需要根据自己的需求进行修改和适配。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值