Wednesday, March 7, 2012

Insert query problem

I am getting this message
"Msg 128, Level 15, State 1, Line 2
The name 'A0000000000 B00C3124901 C00200603071' is not permitted in this
context. Only constants, expressions, or variables allowed here. Column name
s
are not permitted."
when i execute below query in store procedure which dynamic sql query and i
guess because @.strA and strB are string data might cuase problem as in vb.ne
t
i put single quote around it like values(' " & strA & " ',' "& strB " ')"
vb.net code... but i dont know that thing in sql server 2005 as it use singl
e
quote any body has any idea thanks
declare strA nvarchar(20), strB nvarchar(60)
@.sql='INSERT INTO [Dev Work].[dbo].[tbltemp](A,B) VALUES(' + @.strA + ','
+ @.strB + ')'how do you want the query string to look like?
is
'A0000000000 B00C3124901 C00200603071'
the value you are inserting
in that case
do this
'''A0000000000 B00C3124901 C00200603071'''
its 3 single quotes.
it would be better if you can try this out in query analyzer and find the
problem.
and why are you using dynamic SQL.
Can you paste the actual insert script that you are trying to build?
"amjad" wrote:

> I am getting this message
> "Msg 128, Level 15, State 1, Line 2
> The name 'A0000000000 B00C3124901 C00200603071' is not permitted in this
> context. Only constants, expressions, or variables allowed here. Column na
mes
> are not permitted."
> when i execute below query in store procedure which dynamic sql query and
i
> guess because @.strA and strB are string data might cuase problem as in vb.
net
> i put single quote around it like values(' " & strA & " ',' "& strB " ')"
> vb.net code... but i dont know that thing in sql server 2005 as it use sin
gle
> quote any body has any idea thanks
> declare strA nvarchar(20), strB nvarchar(60)
> @.sql='INSERT INTO [Dev Work].[dbo].[tbltemp](A,B) VALUES(' + @.strA + ','
> + @.strB + ')'|||amjad (amjad@.discussions.microsoft.com) writes:
> I am getting this message
> "Msg 128, Level 15, State 1, Line 2
> The name 'A0000000000 B00C3124901 C00200603071' is not permitted in this
> context. Only constants, expressions, or variables allowed here. Column
> names are not permitted."
> when i execute below query in store procedure which dynamic sql query
> and i guess because @.strA and strB are string data might cuase problem
> as in vb.net i put single quote around it like values(' " & strA & " ','
> "& strB " ')" vb.net code... but i dont know that thing in sql server
> 2005 as it use single quote any body has any idea thanks
> declare strA nvarchar(20), strB nvarchar(60)
> @.sql='INSERT INTO [Dev Work].[dbo].[tbltemp](A,B)
> VALUES(' + @.strA + ',' + @.strB + ')'
There is no quoting delimiters in the generated SQL command.
But you should not generate SQL in this way. First of all, it's a little
difficult to see why you would need to use dynamic SQL at all in a stored
procedure for a simple INSERT command. What's wrong with:
INSERT INTO [Dev Work].[dbo].[tbltemp](A,B) VALUES(@.strA, @.strB)
If there is more to it that you don't show, so dynamic SQL indeed is
necessary, you should use sp_executesql instead. This saves you from
the syntactic hassle you are running into now.
I have a longer article where I discuss dynamic SQL in general, and also
extensively cover sp_executesql. You find it on
http://www.sommarskog.se/dynamic_sql.html.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||thanks three quote solved the problem as i am doing some string manipulation
and validation on that data and put invalide data into temp table where i am
doing dynamic sql to insert one by one record. one thing more is their any
mod function in sql server like excel or vb thanks
"Omnibuzz" wrote:
> how do you want the query string to look like?
> is
> 'A0000000000 B00C3124901 C00200603071'
> the value you are inserting
> in that case
> do this
> '''A0000000000 B00C3124901 C00200603071'''
> its 3 single quotes.
> it would be better if you can try this out in query analyzer and find the
> problem.
> and why are you using dynamic SQL.
> Can you paste the actual insert script that you are trying to build?
> --
>
>
> "amjad" wrote:
>|||No sweat :)
--
"amjad" wrote:
> thanks three quote solved the problem as i am doing some string manipulati
on
> and validation on that data and put invalide data into temp table where i
am
> doing dynamic sql to insert one by one record. one thing more is their any
> mod function in sql server like excel or vb thanks
> "Omnibuzz" wrote:
>

No comments:

Post a Comment