[dba-SQLServer] SPROC Help SELECT and INSERT.

Asger Blond ab-mi at post3.tele.dk
Sun Sep 28 14:59:48 CDT 2008


Darryl,

This may be what you need:

INSERT INTO tblTarget (FYP_Imported, SourceKeyID, Alpha, Beta, Gamma, FYP,
Prog) 
SELECT @FYP_imported, SourceKeyID, Alpha, Beta, Gamma, FYP, Prog 
FROM tblSOURCE 
WHERE FYP = @FYP AND Prog = @Prog

Asger

-----Oprindelig meddelelse-----
Fra: dba-sqlserver-bounces at databaseadvisors.com
[mailto:dba-sqlserver-bounces at databaseadvisors.com] På vegne af Darryl
Collins
Sendt: 28. september 2008 02:29
Til: 'Discussion concerning MS SQL Server'
Emne: Re: [dba-SQLServer] SPROC Help SELECT and INSERT.


Hi guys and gals,

A bit of advice if you don't mind. Trying to write a SPROC and know this
should be easy and I think I am making it way too complicated.

I have data in one table and want to copy and insert into another table
(identical fields pretty much).  So for each example

tblSource
'-------------------
SourceKeyID (PK)
Alpha
Beta
Gamma
FYP
Prog

so: SQL would be

SELECT * FROM tblSource
WHERE FYP = @FYP AND Prog = @Prog

or maybe

SELECT
SourceKeyID,
Alpha,
Beta,
Gamma,
FYP,
Prog
FROM tblSource
WHERE FYP = @FYP AND Prog = @Prog

Then I want to insert it in to the tblTarget table.

tblTarget
'-------------------
TargetKeyID ' Auto generated
FYP_Imported ' Needs to be written as a variable (eg 75)
SourceKeyID (FK) ' The following all come from the source table
Alpha
Beta
Gamma
FYP
Prog

Something like

INSERT INTO tblTarget
FYP_Imported = @FYP_imported
SourceKeyID,
Alpha,
Beta,
Gamma,
FYP,
Prog

.

Anyway, I can't seem to get it to work so I am clearly fouling something up
*sigh* any advice?

cheers
Darryl.






More information about the dba-SQLServer mailing list