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)
 N prefix

Author  Topic 

rock1233
Starting Member

4 Posts

Posted - 2007-12-22 : 15:21:27
Hello
please i need your help with this problem:
i have the following INSERT statement:

insert into table1
values (N'ggg')

and it is working fine, but when i try to put a variable string i get an error:

declare @v as nvarchar(10)
set @v = 'ggg'
insert into table1
values (N@v)

this one doesn't work. so please can u tell me how to use variable strings with the N prefix ?
thank you .

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-12-22 : 15:43:55
declare @v as nvarchar(10)
set @v = N'ggg'
insert into table1
values (@v)


elsasoft.org
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-12-22 : 17:52:21
The other thing is that while you set @v = 'ggg'

the @v returned with
Print @v would NOT contain the single quotes, so the reason your values(N@v) does not work is that it tries inserting
Nggg instead of N'ggg'

If you want to include the single quotes in your @v you "could" do it like:

Set @V = ''ggg'' (using TWO single quotes)

However, it is far more practical to use jezemines example.



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-12-22 : 19:26:50
quote:
Originally posted by dataguru1971


If you want to include the single quotes in your @v you "could" do it like:

Set @V = ''ggg'' (using TWO single quotes)



no, if you want to include the quotes in the string, you'd need 3 on each side:

set @v=N'''ggg'''




elsasoft.org
Go to Top of Page

rock1233
Starting Member

4 Posts

Posted - 2007-12-23 : 09:46:22
thanks but this doesn't solve my problem because i still have to work with constants which means i still have to put a fix string ('ggg') for example. but what i want is to put a variable string (a table column for example or @v )
so how can i use the N prefix with a variable ?
i tried to declare the whole insert statement as a string and use it in an EXEC statement (dynamic sql) but i got an error.
so do u have any idea how to solve that ?
thanks again for ur help.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-12-23 : 11:10:01
not sure what you mean. perhaps this:

insert into table1
values (convert(nvarchar(10), @v))


elsasoft.org
Go to Top of Page
   

- Advertisement -