Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, can anyone help me..i wanna insert files from a table to another table. the problem is, when i try to insert more than 1 file with the same cmpntid, it will only insert 1 file only since I generate only one primary key once the stored procedure is running. how do i alter my code so that i can insert more than 1 file means to generate another primary key (ComplaintFileID) for each additional file uploaded..? here is my stored procedure


SQL
DECLARE @MaxFile nvarchar(50)
DECLARE @MaxFileID nvarchar(50)


SELECT @MaxFileID = ISNULL(MAX(ISNULL(RNo,0)),0)+1 FROM Enforcement.Aduan.ComplaintFiles
SET @MaxFile = 'F' + @MaxFileID
	

INSERT INTO DB_B.CmpntFiles  (ComplaintFileID, ComplaintID , FileName ,Description ,FileExtension , FileType , Cid, Cdt)
SELECT @MaxFile, CmpntID, FileName, Description, FileExtension, FileType, Cid, Cdt FROM DB_A.ComplaintFiles 
WHERE CmpntID = @CmpntID


here are my table structure (involve table from different database):

DB_A:Table CmpntFiles
CmpntID
FileName
Description
FileExtension
FileType
Cid
Cdt



DB_B:Table CmpntFiles

ComplaintFileID
ComplaintID
FileName
Description
FileExtension
FileType
Cid
Cdt
Posted
Updated 31-Jul-13 21:34pm
v3
Comments
ArunRajendra 31-Jul-13 23:56pm    
Try Row_number option. Here is a link http://msdn.microsoft.com/en-us/library/ms186734.aspx
Mare7 1-Aug-13 0:24am    
i'm sorry but i always have problem understanding anything from that same website..huhu. can you explain it to me or give me another link related please..
ArunRajendra 1-Aug-13 1:59am    
Can you give me the table structure?
Maciej Los 1-Aug-13 3:11am    
Good question!
I would like to see a definition too.
Mare7 1-Aug-13 3:35am    
Hi, i already updated my question with my table structure

Have a look at the definition of table:
SQL
CREATE TABLE [Aduan].[ComplaintFiles] (
    [ComplaintFileID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    ...

I would suggest you to omit ComplaintFileID field in your INSERT statement because it is IDENTITY[^] field. This mean that value for each new row is added and increases based on previous value ;)

SQL
INSERT INTO DB_B.CmpntFiles  (ComplaintID , FileName ,Description ,FileExtension , FileType , Cid, Cdt)
SELECT CmpntID, FileName, Description, FileExtension, FileType, Cid, Cdt FROM DB_A.ComplaintFiles
WHERE CmpntID = @CmpntID


Are we clear?
 
Share this answer
 
Comments
Mare7 1-Aug-13 5:04am    
tq very much. ill try rewrite it in my code and see the result soon. i'll reply here again and vote once i got the result. thx again..i really appreciate it^^
Try Something Like this...
SQL
INSERT INTO TableName (PrimaryKeyColumn,Columns...)
SELECT @Id+ROW_NUMBER() Over(Order by ColumnNames), Columns from TableName 
WHERE Cond'n

SQL
DECLARE @MaxFile nvarchar(50), @MaxFileID nvarchar(50)

SELECT @MaxFileID = ISNULL(MAX(ISNULL(RNo,0)),0) FROM Enforcement.Aduan.ComplaintFiles
SET @MaxFile = 'F' + @MaxFileID

INSERT INTO Enforcement.Aduan.ComplaintFiles (ComplaintFileID, ComplaintID, FileName, Description, FileExtension, FileType, Cid, Cdt)
SELECT @MaxFile+ Cast(ROW_NUMBER() Over(Order by ComplaintID) as Nvarchar), ComplaintID, FileName, Description, FileExtension, FileType, Cid, Cdt FROM saoCmpntFile
WHERE CmpntID = @CmpntID

Pls Check this Links: Primary Keys -W3 Schools
Primary and Foriegn Keys MSDN
 
Share this answer
 
v5
Comments
Maciej Los 1-Aug-13 3:10am    
Raja, i think that OP doesn't understand how identity field works...
Raja Sekhar S 1-Aug-13 3:17am    
You may be correct Maciej Los. i will Provide some links for his Learning..
Mare7 1-Aug-13 3:36am    
i would really appreciate that. it would really helps me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900