在 varchar 变量 SQL Server 2008 中存储单引号 [英] Storing single quotes in varchar variable SQL Server 2008

查看:24
本文介绍了在 varchar 变量 SQL Server 2008 中存储单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在 SQL Server 2008 中存储单引号.我正在构建多个报告,所有这些报告都完全相同,只是它们仅在我选择的代码上有所不同.例如,一份报告使用代码abc"、def"、ghi",而另一份报告使用代码jkl"、mno"、pqr".我想减少我必须制作的存储过程的数量,我可以在报告上设置一个参数来选择要运行的报告类型.基于此,我会使用正确的代码.所以我打算将这些代码存储在一个 varchar 变量中.以下是我希望的功能:

I was wondering if there is a way to store single quote marks in SQL Server 2008. I am building several reports up and all these reports are the exact same, except they only differ in the codes I am selecting upon. For example, one report uses codes 'abc', 'def', 'ghi' and another report uses codes 'jkl', 'mno', 'pqr'. I was thinking to reduce the number of Stored Procedures I will have to make, I could make a parameter on the report to choose which type of report to run. Based off of that, I would use the correct codes. So I was going to store these codes in a varchar variable. Below is the functionality I was hoping for:

DECLARE @codes1 varchar, @codes2 varchar
SET @codes1 = ''abc', 'def', 'ghi''
SET @codes2 = ''jkl', 'mno', 'pqr''

然后,我将根据用户选择的参数使用正确的 varchar 变量.唯一的问题是设置变量,因为字符串中将包含单引号(该字符串将用于 SQL 'IN' 语句中,这就是存在单引号的原因).

Then, I was going to use the proper varchar variable based on the parameter the user chooses. The only problem is setting the variables since the string will have single quotes in it (the string will be used in an SQL 'IN' statement, thats the reason for the single quotes being present).

推荐答案

像这样.是的,奥德是正确的.对此的正确术语是转义".您可以通过将 ''

Like this. Yes Oded is correct. The proper terminology for this is 'escaping'. You can escape a single quote ' by doubling it up ''

DECLARE @codes1 varchar(50), @codes2 varchar(50)
SET @codes1 = '''abc'', ''def'', ''ghi'''
SET @codes2 = '''jkl'', ''mno'', ''pqr'''

这篇关于在 varchar 变量 SQL Server 2008 中存储单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆