Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Learn sp_executesql

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-11-05 : 16:10:39
To learn sp_executesql, I create a small code. It works as below.

CREATE TABLE [dbo].[#A](
[authno] [nchar](10) NULL,
[Authname] [nvarchar](50) NULL
) ON [PRIMARY]

INSERT INTO #A (AUTHNO, AUTHNAME)
VALUES('444' + '~', 'EEE')

Once run sp_executesq, got an error said

Incorrect syntax near '444'. How to fix it?

CREATE TABLE [dbo].[#A](
[authno] [nchar](10) NULL,
[Authname] [nvarchar](50) NULL
) ON [PRIMARY]

DECLARE @sql nvarchar(200)
set @sql = N'
INSERT INTO #A (AUTHNO, AUTHNAME)
VALUES('444' + '~', 'EEE')'
exec sp_executesql @sql

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-05 : 16:16:07
inside you have to mask a quote with another quote.
''444''+''~'',''EEE''


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-06 : 03:00:46
Refer this to know how single quotes work in SQL Server
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -