Modify statement to run sp_executesql
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: microsoft.public.sqlserver.programming
From: "aspfun" <u53138@uwe>
Date: Sat, 07 Nov 2009 13:42:35 GMT
Local: Sun, Nov 8 2009 12:42 am
Subject: Modify statement to run sp_executesql
To learn sp_executesql, I create a small code. It works fine 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
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: microsoft.public.sqlserver.programming
From:
"Farmer" <some... @somewhere.com>
Date: Sat, 7 Nov 2009 09:56:26 -0500
Local: Sun, Nov 8 2009 1:56 am
Subject: Re: Modify statement to run sp_executesql
How about this DECLARE @sql nvarchar(200) set @sql = N' INSERT INTO #A (AUTHNO, AUTHNAME) VALUES(''444'' + ''~'', ''EEE'')' exec sp_executesql @sql
"aspfun" <u53138@uwe> wrote in message
news:9ead6e00d8e19@uwe ...
> To learn sp_executesql, I create a small code. It works fine 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
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: microsoft.public.sqlserver.programming
From:
Erland Sommarskog <esq... @sommarskog.se>
Date: Sat, 7 Nov 2009 17:53:56 +0000 (UTC)
Local: Sun, Nov 8 2009 4:53 am
Subject: Re: Modify statement to run sp_executesql
aspfun (u53138@uwe) writes:
> To learn sp_executesql, I create a small code. It works fine 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?
Dynamic SQL is an advanced feature, and if you feel obliged to ask this question, you should probably defer working with dynamic SQL, until you master the basics better. That said, I have an article on my web site, which covers dynamic SQL in depth: http://www.sommarskog.se/dynamic_sql.html .
-- Erland Sommarskog, SQL Server MVP, esq... @sommarskog.se
Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: microsoft.public.sqlserver.programming
From:
"John Bell" <jbellnewspo... @hotmail.com>
Date: Sat, 7 Nov 2009 20:45:30 -0000
Local: Sun, Nov 8 2009 7:45 am
Subject: Re: Modify statement to run sp_executesql
To expand on this, when you need a quote in a string it has to be escaped with another single quote. John
"Farmer" <some
... @somewhere.com> wrote in message
news:eaZl7p7XKHA.3612@TK2MSFTNGP02.phx.gbl ...
> How about this
> DECLARE @sql nvarchar(200) > set @sql = N' > INSERT INTO #A (AUTHNO, AUTHNAME) > VALUES(''444'' + ''~'', ''EEE'')' > exec sp_executesql @sql
> "aspfun" <u53138@uwe> wrote in message news:9ead6e00d8e19@uwe ... >> To learn sp_executesql, I create a small code. It works fine 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
You must
Sign in before you can post messages.
You do not have the permission required to post.