Current location: Hot Scripts Forums » Other Discussions » Database » MSSQL INSERT INTO from SELECT


MSSQL INSERT INTO from SELECT

Reply
  #1 (permalink)  
Old 09-23-09, 01:06 PM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
MSSQL INSERT INTO from SELECT

We currently have an INSERT query to insert data from a SELECT from an OPENQUERY. This works well but what we want to do is add a column with a fixed value which we will use as an identifier.

IS there anyway we can do this to the below query?

Want to add a column called [PHPCheck] and load in the fixed value from @PHPCheck

Code:
--Generate ADP Query
Set @ADPQuery1 ='INSERT INTO ADPDL_Stage ('
Set @ADPQuery2 ='[DIVMY],[AC_CODE],[Value],[ADP_Desc]) Select * from OPENQUERY(ADP,'
Set @ADPQuery3 = '''SELECT NL_00_NominalDepartmentCodes.AccountingDivision, NL_00_NominalAccountCodes.AccountCode, NL_00_NominalAccountCodes.BroughtForward001+NL_00_NominalAccountCodes.CurrentPostings001 BFandCP, NL_00_NominalExpenseCodes.Description001
FROM NL_00_NominalAccountCodes NL_00_NominalAccountCodes, NL_00_NominalDepartmentCodes NL_00_NominalDepartmentCodes, NL_00_NominalExpenseCodes NL_00_NominalExpenseCodes
WHERE NL_00_NominalDepartmentCodes.CostCentre = NL_00_NominalAccountCodes.CostCentre AND NL_00_NominalAccountCodes.ExpenseCode = NL_00_NominalExpenseCodes.ExpenseCode  
AND NL_00_NominalDepartmentCodes.AccountingDivision = '
Set @ADPQuery4 = 
'AND (Broughtforward001 <> 0.00 AND CurrentPostings001 <> 0.00)
ORDER BY NL_00_NominalAccountCodes.AccountCode'');'

-- Inserting data from the adptemp table
exec (@ADPQuery1 + @ADPQuery2  + @Adpquery3 + @adpdiv + @ADPQuery4)
Any help is greatly appreciated

Scott
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-24-09, 04:27 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
NVM I've found out how to do this by changing the SELECT * FROM to SELECT column1,column2....'value' FROM

Code:
--Generate ADP Query
Set @ADPQuery1 ='INSERT INTO ADPDL_Stage ('
Set @ADPQuery2 ='[DIVMY],[AC_CODE],[Value],[ADP_Desc],[Month],[Year],[PHPCheck]) SELECT AccountingDivision,AccountCode,BFandCP,Description001,'''+@ADPMth+''','''+@ADPYear+''','''+@PHPCheck+''' FROM OPENQUERY(ADP,'
Set @ADPQuery3 = '''SELECT NL_00_NominalDepartmentCodes.AccountingDivision, NL_00_NominalAccountCodes.AccountCode, NL_00_NominalAccountCodes.BroughtForward001+NL_00_NominalAccountCodes.CurrentPostings001 BFandCP, NL_00_NominalExpenseCodes.Description001
FROM NL_00_NominalAccountCodes NL_00_NominalAccountCodes, NL_00_NominalDepartmentCodes NL_00_NominalDepartmentCodes, NL_00_NominalExpenseCodes NL_00_NominalExpenseCodes
WHERE NL_00_NominalDepartmentCodes.CostCentre = NL_00_NominalAccountCodes.CostCentre AND NL_00_NominalAccountCodes.ExpenseCode = NL_00_NominalExpenseCodes.ExpenseCode  
AND NL_00_NominalDepartmentCodes.AccountingDivision = '+@ADPDiv+''
Set @ADPQuery4 = 
'AND (Broughtforward001 <> 0.00 AND CurrentPostings001 <> 0.00)
ORDER BY NL_00_NominalAccountCodes.AccountCode'');'

-- Inserting data from the adptemp table
exec (@ADPQuery1 + @ADPQuery2  + @Adpquery3 + @ADPQuery4)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 09:34 PM
Why won't select list item insert into database? gavacho PHP 2 12-05-08 07:38 PM
Select List insert into DB The Omega JavaScript 1 01-12-05 01:51 PM
MySQL table problem perleo PHP 9 12-16-03 02:16 PM


All times are GMT -5. The time now is 07:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.