您的位置:首页 > 数据库

insert当 sql语句里面有变量 为字符类型的时候 要3个单引号

2014-09-26 15:04 351 查看
set @InsertStr='INSERT INTO [dbo].[T_SchoolPercentMonth]([SchoolID],[MonthOfYear],[PercentNum]) VALUES('''+cast(@SchoolID as nvarchar)+''','''+convert(char(7),getdate(),20) +''','+cast(@PercentNum as nvarchar)+')';

引用字符 加一对单引号. 'select * from tb where col=' + 'abcd' + ' and true'
字符如果是变量值,再加两对单引号. 'select * from tb where col='''+ @colval+''' and true'
如果字符是语句的结尾,加四个单引号.'select * from tb where col=''' +@colval+''''
如果引用的是数值,则只要加一对: 'select * from tb where col='+ @intval +' and true'
如果引用的数值,且为语句结尾,则后面的单引号也不要:'select * from tb where col=' +@intval


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐