views:

190

answers:

4

I am using SQL Server 2008 and developing a project which is in maintenance phase.

I want to insert record in a table whose primary key is an Integer but not an identity. e.g. table name is tblFiles and fields are ID, FileName, FileContent.

Actually that table is in use so I don’t want to make any schema change in it. And I want the key after row insertion because I have to put that in another table. Existing values in the Id column are different integer, means not in sequence.

So I want the query that also returns me the Id value. So I want to insert only FileName and FileContent and some sort of sql to whom I can embed in my insert query which insert a unique Id and also send me that id

+4  A: 

Well, if it's not an IDENTITY field - don't you already have to specify the "ID" in your insert for it to succeed ? If so - you already have the ID! Or what am I missing? Is the ID determined by a trigger or something??

If so, try this query:

INSERT INTO dbo.tblFiles(FileName, FileContent)
OUTPUT inserted.ID
VALUES ('yourfile.name', 'your contents')

This should return the newly inserted ID from the INSERT query.

Marc

marc_s
I think he's looking for an auto-generated ID without changing the schema to do so.
womp
@womp: OK, in that case, I'd probably add a helper table with an INT IDENTITY field and insert a dummy row there and get the new autogenerated ID, and then use that in the `tblFiles` table
marc_s
@Marc; Thanks, I have posted my answer, in that I have posted my final qyuery, your answer help me to build final query, I am marking your answer.
Azhar
A: 

You could create a unique integer, not so elegantly, using

SELECT MAX(ID) + 1 FROM tblFiles

And simply return this from your query or sproc as the case maybe. Otherwise follow as marc_s says if it is known already.

UPDATE: have to say, rather than this fudge as requested, I would strongly recommend pushing back hard and getting table changed so this is an identity column, as this is what is. all answers so far are simply fudges, mine especially.

dove
this is really really bad design in a busy multi-user system - you're absolutely **guaranteed** to have duplicates after no time at all......
marc_s
@marc_s take your point but would need to know if this is a busy system or as is likely there is a table lock on this insert. if i were in his shoes, I'd simply push for identity to be applied rather than any of these fudges. think I'll put this in answer....
dove
@marc_s btw, if it's a primary key, duplicates will be impossible, but admittedly you will get an error on trying to insert if there are not locks in place.
dove
@dove: true- a primary key would prevent duplicates - but you'd have errors that might be hard to find and track down. And I agree - just simply using IDENTITY would be BY FAR the easiest solution...
marc_s
A: 

Change the Columns Identity Specification > Is Identity to Yes.

The after inserting into the table you can

Select SCOPE_IDENTITY()

to get the integer that was just added and return this in your SP.

If you really can't edit the database schema maybe you could add another table to the database that has two columns called ID and CurrentDate. Make the ID column an Identity. In your code insert into this table first select SCOPE_IDENTITY() and then use the integer returned to insert as the ID in your tblFles table.

P.S. Stop prefixing your table with tbl that's so 1999. :)

Martin Beeby
pps: shouuld we start prefixing tables with "T_" instead?? ;-)
marc_s
come on that's so 2001
Martin Beeby
+1  A: 

so my final query look like...

Insert into dbo.tblData (Id, FName, LName) 
output inserted.Id
values ((SELECT MAX(ID) + 1 FROM dbo.tblData), 'xyz', 'abc')
Azhar