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

时间:2022-12-08 06:46:32

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