Wednesday, March 7, 2012

how to get more then one isentity

i have few tables and in one of them i need to insert

the PK of sometable1 as FK to sometable3 and

the PK of sometable2 as Fk to sometable3

i have this for while:

select ident_current("dbo.Images") as img

select ident_current("dbo.Articals")as art

insert into dbo.ArticalsImages(ImageID,ArticalCode)

values (img,art)

but it give me error :

The name "img" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

why is it?

if there is other better way its greater|||

May be you can try this.

insert into dbo.ArticalsImages(ImageID,ArticalCode)

select ident_current("dbo.Images"), ident_current("dbo.Articals")

Or you can declare those variables (img and art) and assign value to the variables

select img=ident_current("dbo.Images")

and then insert the values.

|||

Roy:

Try this instead:


declare @.img integer
declare @.art integer

select ident_current('dbo.Images') as @.img
select ident_current('dbo.Articals')as @.art


insert into dbo.ArticalsImages(ImageID,ArticalCode)
values (@.img,@.art)

This may fix the syntax errors; however, I have a good deal of doubt about using the IDENT_CURRENT function. I would suggest looking up @.@.IDENTITY in books online. What I would normally suggest is that you track what is going on and use SCOPE _IDENTITY instead of IDENT_CURRENT. With IDENT_CURRENT it is possible for an insert by a completely different process to become the keys that you would be inserting into your target table. Can you describe a little more what you are doing before you go to insert the row into "ArticalsImages"?

No comments:

Post a Comment