一、sp_executesql 语法
功能描述:执行可多次重用的Transact-SQL语句或批处理,或动态生成的语句或批处理。Transact-SQL语句或批处理可以包含嵌入参数。
语法:
- -- SQL Server、Azure SQL数据库、Azure SQL数据仓库、并行数据仓库的语法
- sp_executesql [ @stmt = ] statement
- [
- { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' }
-
- { , [ @param1 = ] 'value1' [ ,...n ] }
- ]
参数:
[ @stmt= ] statement
| 1 |
|
[ @params= ] N'@parameter_name data_type [ ,... n ] '
[ @param1= ] 'value1'
[ OUT | OUTPUT ]
n
返回值:
0(成功)或非零(失败)
二、使用案例
- declare @sql nvarchar(1000),@tempScore decimal(18,2);
- set @sql=N'select @tempScore=sum(num'+rtrim(ltrim(str(@dataid)))+') from DataNum where zdposition like '''+@zdposition+'_%'' and CreateTime>= '''++@start++''' and CreateTime<= '''+@end+'''';
- --print @sql;
- exec sp_executesql @sql,N'@tempScore decimal(18,2) output', @tempScore output;
更多: