当前位置:首页 > SQL中存储过程中SQL语句的单引号和双引号问题
thename & ?’)?
这里Insert into mytable(username) values(‘是张红前面的部分,thename是字符串变量,’)是张红后面的部分。将thename变量替换成张红,再用将三段连接起来,就变成了 strsql=?Insert into mytable(username) values(‘张红’)?。
如果要插入两个字段,如姓名为?张红?,类型为?学生? strsql=?Insert into mytable(username,leixing) values(‘张红’,’学生’)?
如果现在姓名是一个变量thename,类型也是一个变量thetype,则写成:
strsql=?Insert into mytable(username,leixing) values(‘?&thename&?,?&thetype &?’)?
和第一个例子一样,将thename和thetype替换后,再用连接符,就连接成和上面一样的字符串了。 2.插入数字型
假如插入一个年龄为12的记录,要注意数字不用加单撇号:
strsql=?Insert into mytable(age) values(12)? 如果现在年龄是一个变量theage,则为:
strsql=?Insert into mytable(age) values(? & theage & ?)? 这里Insert into mytable(age) values(是12前面的部分,theage
是年龄变量,)是12后面部分。将theage替换,再用连接符将三部分连接起来,就变为了和上面一样的字符。
3.插入日期型
日期型和字符串型类似,但是要将单撇号替换为#号。(不过,Access数据库中用单撇号也可以) strsql=?Insert into mytable(birthday) values(#1980-10-1#)? 如果换成日期变量thedate
strsql=?Insert into mytable(birthday) values(#? & thedate & ?#)?
4.插入布尔型
布尔型和数字型类似:只不过只有两个值 True和False,如:
strsql=?Insert into mytable(marry) values(True)? 如果换成布尔变量themarry
strsql=?Insert into mytable(birthday) values(? & themarry& ?)? 5.综合示例
插入一个姓名为张红,年龄为12,出生日期为1970-6-8的记录
strsql=?Insert into mytable(username,age,birthday)
values(‘张红’,12,#1970-6-8#)?
仔细注意上式:因为姓名是字符串,所以张红两边加了单撇号;年龄是数字,所以没有加单撇号。如果换成字符串变量thename、数字变量theage和日期变量thebirthday,则变为: strsql=?Insert into mytable(username,age,birthday) values(‘?& thename &?’,?& theage&?,#?& thebirthday & ?#)?
总之,替换变量,再连接后要完成和上边一样的字符串。 6.小窍门 (1)插入字符串型
把下面的语句题换成变量的写法:
strsql=?Insert into mytable(username) values(‘张红’)? 第一步:先把张红抹去,在原位置加两个引号
strsql=?Insert into mytable(username) values(‘??’)? 第二步:在中间添加两个连接符
strsql=?Insert into mytable(username) values(‘?& &?’)? 第三步:把变量写在两个连接符之间
strsql=?Insert into mytable(username) values(‘?& thename &?’)?
(2)插入数值/布尔型
把下面的语句题换成变量的写法: strsql=?Insert into mytable(age) values(12)? 第一步:先把12抹去,在原位置加两个引号 strsql=?Insert into mytable(username) values(??)? 第二步:在中间添加两个连接符
strsql=?Insert into mytable(username) values(?& &?)? 第三步:把变量写在两个连接符之间
strsql=?Insert into mytable(username) values(?& theage &?)?
(3)插入日期型
把下面的语句题换成变量的写法: strsql=?Insert into mytable(birthday) values(#1980-10-1#)?
第一步:先把1980-10-1抹去,在原位置加两个引号 strsql=?Insert into mytable(username) values(#??#)? 第二步:在中间添加两个连接符
strsql=?Insert into mytable(username) values(#?& &?#)? 第三步:把变量写在两个连接符之间
strsql=?Insert into mytable(username) values(#?& thedate &?#)?
共分享92篇相关文档