如何用单引号插入文本sql server 2005

发布于 2024-07-17 20:08:59 字数 53 浏览 4 评论 0原文

我想插入带单引号的文本 例如 john 到 sql server 2005 数据库中的表

I want to insert text with single quote
Eg john's to table in sql server 2005 database

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

勿忘心安 2024-07-24 20:08:59

正如 Kirtan 指出的那样,使用附加单引号来转义单引号
如果您尝试通过 sp_executesql 执行动态 sql(这首先不是一个好主意),那么下面的代码将适合您

sp_executesql N'INSERT INTO SomeTable (SomeColumn) VALUES (''John''''s'')'

Escape single quote with an additional single as Kirtan pointed out
And if you are trying to execute a dynamic sql (which is not a good idea in the first place) via sp_executesql then the below code would work for you

sp_executesql N'INSERT INTO SomeTable (SomeColumn) VALUES (''John''''s'')'
谈情不如逗狗 2024-07-24 20:08:59

答案实际上取决于您如何执行 INSERT。

如果您指定 SQL 文字,则需要使用双击方法:

-- Direct insert
INSERT INTO Table1 (Column1) VALUES ('John''s')

-- Using a parameter, with a direct insert
DECLARE @Value varchar(50)
SET @Value = 'John''s'
INSERT INTO Table1 (Column1) VALUES (@Value)

-- Using a parameter, with dynamic SQL
DECLARE @Value varchar(50)
SET @Value = 'John''s'
EXEC sp_executesql 'INSERT INTO Table1 (Column1) VALUES (@p1)', '@p1 varchar(50)', @Value

如果您从代码执行 INSERT,请使用参数:

// Sample ADO.NET
using (SqlConnection conn = new SqlConnection(connectionString)) {
    conn.Open();
    using (SqlCommand command = conn.CreateCommand()) {
        command.CommandText = "INSERT INTO Table1 (Column1) VALUES (@Value)";

        command.Parameters.AddWithValue("@Value", "John's");

        command.ExecuteNonQuery();
    }
}

如果您的数据包含直接或间接的用户输入,使用参数。 参数可防止 SQL 注入攻击。 永远不要通过用户输入构建动态 SQL。

The answer really depends on how you are doing the INSERT.

If you are specifying a SQL literal then you need to use the double-tick approach:

-- Direct insert
INSERT INTO Table1 (Column1) VALUES ('John''s')

-- Using a parameter, with a direct insert
DECLARE @Value varchar(50)
SET @Value = 'John''s'
INSERT INTO Table1 (Column1) VALUES (@Value)

-- Using a parameter, with dynamic SQL
DECLARE @Value varchar(50)
SET @Value = 'John''s'
EXEC sp_executesql 'INSERT INTO Table1 (Column1) VALUES (@p1)', '@p1 varchar(50)', @Value

If you are doing the INSERT from code, use parameters:

// Sample ADO.NET
using (SqlConnection conn = new SqlConnection(connectionString)) {
    conn.Open();
    using (SqlCommand command = conn.CreateCommand()) {
        command.CommandText = "INSERT INTO Table1 (Column1) VALUES (@Value)";

        command.Parameters.AddWithValue("@Value", "John's");

        command.ExecuteNonQuery();
    }
}

If your data contains user-input, direct or indirect, USE PARAMETERS. Parameters protect against SQL Injection attacks. Never ever build up dynamic SQL with user-input.

执手闯天涯 2024-07-24 20:08:59

这对我有用:

  INSERT INTO [TABLE]
  VALUES ('text','''test.com''', 1)

基本上,您可以使用要插入的单引号并将其替换为两个引号。 因此,如果要插入文本字符串 ('text') 并在其周围添加单引号,则为 ('''text''')。 希望这可以帮助。

This worked for me:

  INSERT INTO [TABLE]
  VALUES ('text','''test.com''', 1)

Basically, you take the single quote you want to insert and replace it with two. So if you want to insert a string of text ('text') and add single quotes around it, it would be ('''text'''). Hope this helps.

欲拥i 2024-07-24 20:08:59
INSERT INTO Table1 (Column1) VALUES ('John''s')

或者您可以使用存储过程并将参数传递为 -

usp_Proc1 @Column1 = 'John''s'

如果您使用的是 INSERT 查询而不是存储过程,则必须用两个引号对引号进行转义,否则如果不这样做也没关系。

INSERT INTO Table1 (Column1) VALUES ('John''s')

Or you can use a stored procedure and pass the parameter as -

usp_Proc1 @Column1 = 'John''s'

If you are using an INSERT query and not a stored procedure, you'll have to escape the quote with two quotes, else its OK if you don't do it.

葵雨 2024-07-24 20:08:59

这个答案适用于 SQL Server 2005、2008、2012。

有时该值有许多单引号。 而不是像上面使用 'John''s' 那样在每个单引号旁边添加一个单引号。 还有一些示例使用 REPLACE 函数来处理值中的多个单引号。

尝试以下操作。 这是一条更新语句,但您也可以在 INSERT 语句中使用它。

SET QUOTED_IDENTIFIER OFF

DECLARE @s VARCHAR(1000)

SET @s = "SiteId:'1'; Rvc:'6'; Chk:'1832'; TrEmp:'150'; WsId:'81'; TtlDue:'-9.40'; TtlDsc:'0'; TtlSvc:'0'; TtlTax:'-0.88'; TtlPay:'0'; TipAmt:'0.00'; SvcSeq:'09'; ReTx:'N'; TraceId:'160110124347N091832';"


UPDATE TransactionPaymentPrompt
set PromptData = @s

from TransactionPaymentPrompt tpp with (nolock)
where tpp.TransactionID = '106627343'

This answer works in SQL Server 2005, 2008, 2012.

At times the value has MANY single quotes. Rather than add a single quote next to each single quote as described above with 'John''s'. And there are examples using the REPLACE function to handle many single quotes in a value.

Try the following. This is an update statement but you can use it in an INSERT statement as well.

SET QUOTED_IDENTIFIER OFF

DECLARE @s VARCHAR(1000)

SET @s = "SiteId:'1'; Rvc:'6'; Chk:'1832'; TrEmp:'150'; WsId:'81'; TtlDue:'-9.40'; TtlDsc:'0'; TtlSvc:'0'; TtlTax:'-0.88'; TtlPay:'0'; TipAmt:'0.00'; SvcSeq:'09'; ReTx:'N'; TraceId:'160110124347N091832';"


UPDATE TransactionPaymentPrompt
set PromptData = @s

from TransactionPaymentPrompt tpp with (nolock)
where tpp.TransactionID = '106627343'
辞旧 2024-07-24 20:08:59

您询问如何在 SQL Server 中转义撇号字符 (')。 上面的所有答案都很好地解释了这一点。

但是,根据具体情况,右单引号字符 (') 可能比较合适。

(不需要转义字符)

-- Direct insert
INSERT INTO Table1 (Column1) VALUES ('John’s')

• 撇号 ( U+0027)

维基百科上的 Ascii 撇号

• 右单引号 (U+2019)

< a href="https://en.wikipedia.org/wiki/List_of_Unicode_characters#Unicode_symbols" rel="nofollow">维基百科上的 Unicode 右单引号

You asked how to escape an Apostrophe character (') in SQL Server. All the answers above do an excellent job of explaining that.

However, depending on the situation, the Right single quotation mark character (’) might be appropriate.

(No escape characters needed)

-- Direct insert
INSERT INTO Table1 (Column1) VALUES ('John’s')

• Apostrophe (U+0027)

Ascii Apostrophe on Wikipedia

• Right single quotation mark (U+2019)

Unicode Right single quotation on Wikipedia

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文