| Author |
Topic |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-01-23 : 10:58:19
|
| Bonjour!I have following tableCREATE TABLE [dbo].[blabla]( [ID] [int] IDENTITY(1,1) NOT NULL, [BranchID] [int] NOT NULL, [TypeID] [int] NOT NULL, [Number] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Quantity] [int] NULL,) ON [PRIMARY]Then I do the followingINSERT INTO dbo.blabla (BranchID ,TypeID ,Number ,Quantity) OUTPUT INSERTED.ID, INSERTED.PRLNumber INTO @Inserted SELECT 1, 2, '01212009-184', CAST(QuantityNeeded AS INT) FROM SM cs INNER JOIN Quantities cq ON cs.TableID = cq.TableIDI need to the insert indentity id of SM table in the @inserted table variable. Been banging my head for too long so I thought I would ask the experts.Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-23 : 11:09:41
|
| the query looks fine. whats the problem you're facing ? also is @Inserted declared before this? |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-01-23 : 11:11:50
|
yes @inserted is a table variable declared earlier ---did not want to bore you with the details---query is good but I need a third column inserted into @inserted table variable.the OUTPUT INSERTED.ID you see for table blabla...I want the id for table SM in the FROM. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-23 : 11:16:50
|
| is ID being inserted into blabla? if not, it wont be available in INSERTED and so you cant retrieve it using OUTPUT |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-01-23 : 11:20:49
|
| no SM.ID is not being inserted into blabla...but how can I retrieveI read that you can doINSERT INTO tOUTPUT INSERT.id, INSERTED.meow, s.IDSELECT x,x,x FROM table s INNER JOIN ....... sort of thingy.Thank you much |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-23 : 11:42:53
|
quote: Originally posted by yosiasz no SM.ID is not being inserted into blabla...but how can I retrieveI read that you can doINSERT INTO tOUTPUT INSERT.id, INSERTED.meow, s.IDSELECT x,x,x FROM table s INNER JOIN ....... sort of thingy.Thank you much
what happened when you tried like above? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-23 : 11:57:10
|
[code]-- Prepare sample dataDECLARE @Master TABLE (MasterID INT IDENTITY(1, 1), Name VARCHAR(20))DECLARE @Slave TABLE (SlaveID INT IDENTITY(1, 1), MasterID INT, Name VARCHAR(20))-- Insert first dummy recordINSERT @Master ( Name )SELECT 'Master 1'-- Insert second dummy recordINSERT @Master ( Name )OUTPUT inserted.MasterID, 'Slave 1'INTO @SlaveSELECT 'Master 2'-- Show resultsSELECT *FROM @MasterSELECT *FROM @Slave[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2009-01-23 : 13:34:41
|
| thank you all!visakh16 I tried like that but no go because I am missing a key to join them with. I had to abandone this approach |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-24 : 10:49:27
|
| ok... |
 |
|
|
|