Escape Character in SQL

丶灬走出姿态 提交于 2019-12-12 13:12:05

问题


Please forgive me as I am a bit of an sql noob. I am trying to do an insert using the following but am having a problem with the apostrophes. I have a lots of records to insert but many have the same problem. Is there a way of escaping them?

INSERT INTO  [dbo].[tb_Chefs] ([ChefHotelID], [HotelID], [ChefID],   
     [Position],  [GroupID])  
VALUES(N'b809a86e-f7f2-45b2-a240-0049f51509d7' ,N'14481', N'624', 
     N'Chef d'atelier', N'331')
GO

Any help much appreciated.


回答1:


'Chef d'atelier' becomes 'Chef d''atelier'

Just double them up

If a character string enclosed in single quotation marks contains an embedded quotation mark, represent the embedded single quotation mark with two single quotation marks.




回答2:


You can also you the ESCAPE clause. This allows you to define your own escape character.




回答3:


Use something like this instead

INSERT INTO [dbo].[tb_Chefs] ([ChefHotelID], [HotelID], [ChefID], [Position], [GroupID])
VALUES("N'b809a86e-f7f2-45b2-a240-0049f51509d7'" ,"N'14481'", "N'624'", "N'Chef d'atelier', N'331'")

OR you could store the values you want to insert in a variable first and then use the variable in the SQL statement instead of the raw value.



来源:https://stackoverflow.com/questions/3329587/escape-character-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!