Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INSERT INTO using SELECT with PARAMETER

Author  Topic 

bbauer1973
Starting Member

3 Posts

Posted - 2010-04-14 : 12:02:10
I am trying to create a payroll application and I would like to take the data from one table and insert it into another with almost the same schema. The first table is basically STORED PAYROLL DETAILS and the second is PAYROLL DETAILS. Basically the STORED DETAILS table will store a list of personnel and related data that is reoccurring on a daily payroll so that the user doesn't have to enter the data everyday. The difference between the two tables is that the DETAILS table has a foreign key of PAYROLL_ID needing to match the PAYROLL table. I tried to write a statement in which I pass the PAYROLL ID from .net control via a parameter like so:

INSERT INTO payroll_details (payroll_id, name, worker_id, job_category)

SELECT FROM payroll_details_stored (@payroll_id, name, worker_id, job_category)

This obviously doesn not work as the parameter is passed form the page and is not selected from the STORED table. Thanks in advance for any help on the correct syntax.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-14 : 12:04:55
INSERT INTO payroll_details (payroll_id, name, worker_id, job_category)
SELECT @payroll_id, name, worker_id, job_category
FROM payroll_details_stored

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bbauer1973
Starting Member

3 Posts

Posted - 2010-04-14 : 12:17:51
Sorry...

I am using that format exactly as you state:

INSERT INTO payroll_details (payroll_id, name, worker_id, job_category)
SELECT @payroll_id, name, worker_id, job_category
FROM payroll_details_stored

But it states that the scalar varibale @parameter is not defined
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-14 : 13:39:09
Well you need to handle the parameter in your .NET code then. I can show you how it's done in T-SQL, but it's different from .NET:

DECLARE @payroll_id int

SET @payroll_id = 999

INSERT INTO payroll_details (payroll_id, name, worker_id, job_category)
SELECT @payroll_id, name, worker_id, job_category
FROM payroll_details_stored

So the next step for you is to figure out how to do that same thing from within .NET.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bbauer1973
Starting Member

3 Posts

Posted - 2010-04-14 : 15:15:44
It works! Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-14 : 16:05:09
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -