Showing posts with label identity. Show all posts
Showing posts with label identity. Show all posts

Friday, March 9, 2012

how to use insert into with IDENTITY ( seed ,increment )

when i insert data from outside
i use insert into ....select...
can i use IDENTITY ( seed ,increment ) in my statement.
for example i have table
aaaaa AAA
bbbbb BBB
ccccc CCC

can i insert into new table with data

1 aaaaa AAA
2 bbbbb BBB
3 ccccc CCC
thanks

You can use cast (or convert)

create table test
(
integerValue int
)
insert into test
select 1
union all
select 232525
union all
select 2325
union all
select 525
union all
select 99232525

select integerValue, cast(integerValue as varchar(10)) as varcharValue
from test

integerValue varcharValue

1 1

232525 232525

2325 2325

525 525

99232525 99232525

|||

Well, first, don't change the meaning of a thread...I already answered it as it was originally asked Smile


create table test
(
value char(5),
otherValue char(3)
)
insert into test
select 'aaaaa','AAA'
union all
select 'bbbbb','BBB'
union all
select 'ccccc','CCC'

You can do this:


select identity(int,1,1) as test2Id,
value,
otherValue
into test2
from test

Or if you just wanted numbers, you can do this (in 2005) to get control over the values.


select row_number() over (order by value) as test3Id,
value,
otherValue
into test3
from test
go

|||sorry, i have change my question. can you answer me.

Wednesday, March 7, 2012

how to use convert data type from int to char in select statement

when i insert data from outside
i use insert into ....select...
can i use IDENTITY ( seed ,increment ) in my statement.
for example i have table
aaaaa AAA
bbbbb BBB
ccccc CCC

can i insert into new table with data

1 aaaaa AAA
2 bbbbb BBB
3 ccccc CCC
thanks

You can use cast (or convert)

create table test
(
integerValue int
)
insert into test
select 1
union all
select 232525
union all
select 2325
union all
select 525
union all
select 99232525

select integerValue, cast(integerValue as varchar(10)) as varcharValue
from test

integerValue varcharValue

1 1

232525 232525

2325 2325

525 525

99232525 99232525

|||

Well, first, don't change the meaning of a thread...I already answered it as it was originally asked Smile


create table test
(
value char(5),
otherValue char(3)
)
insert into test
select 'aaaaa','AAA'
union all
select 'bbbbb','BBB'
union all
select 'ccccc','CCC'

You can do this:


select identity(int,1,1) as test2Id,
value,
otherValue
into test2
from test

Or if you just wanted numbers, you can do this (in 2005) to get control over the values.


select row_number() over (order by value) as test3Id,
value,
otherValue
into test3
from test
go

|||sorry, i have change my question. can you answer me.

Sunday, February 19, 2012

How to use a variable in a SQLCommand?

Hi!

I need to know what can I do to use a variable in theWHEREcondition of the sqlcommand as I show you:

current_user = User.Identity.NameDim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = current_user", cn)
 
Obviously it doesn't work and I need your help. Thanks. 

Sorry this is not the question that I have, sorry.

The question is what can I do to use a variable(User.Identity.Name)in a SelectCommand that I use in a GridView, thanks again.

Something like that, but it doesn't work:

SelectCommand="SELECT id_fax, fecha,destino, propietario, contenido, estado, cola FROM fax WHERE(propietario = <%# User.Identity.Name %>)"

|||
try this, I think this is what you are looking for:
Dim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = "¤t_user, cn)
|||

Hi rubencdl,

You have to use String Concatination, the term String Concatination is use to combine two different strings. In your case you have two different string

first string containt the query

"SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' '"

and other contain the user name

current_user = User.Identity.Name

Now you have to combine these two string in a signle string, and for + operator is use for combination of two string

Dim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' '"

+ current_user + " ' " , cn)

Hope it works

Thank You,

Best Regards,

Muhammad Akhtar Shiekh

 
 
|||

Thanks to both of you.

But this is the answer for the first question, and I need to know the answer for the second, how can I do to use a variable in the SelectCommand of a GridView.

Thanks.

|||

Hi Dear,

I m really sorry dear, i can't understandt he meaning of "SelectCommand of a GridView."Confused

What do u mean by SelectCommand of a GridView? there is no Select Command Property in GridView...

can u please explain again what you really want? because string concatination will be same in each senario...

Thank You

Best Regards,

Muhammad Akhtar Shiekh

|||Sorry, is the SelectCommand of a SQLDataSourceEmbarrassed|||

rubencdl:

Sorry, is the SelectCommand of a SQLDataSourceEmbarrassed

try this in page_load event

SqlDataSource1.SelectCommand = "SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' " + User.Identity.Name + " ' "