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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Insert variables not part of my select

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-06-22 : 08:10:22
When do an insert to a temp table is it possible to insert variables that does not really exist in the "select" statement.

In this case I would like to insert a value for plant_id and plant_name even if they do not exist in the salestkt table. I cannot use a default value since I am looping through 8 different data bases and manually need to control values plant variables.


create table	#PlantTable
(
plant_id char(10) default ' ',
plant_name char(20) default Null,
shift_date varchar(25) default Null,
outside_qty decimal(15,2) default Null,
intercompany_qty decimal(15,2) default Null
)

Insert Into #PlantTable(shift_date, intercompany_qty, outside_qty)
SELECT *
FROM OPENQUERY(ADS_RGDB_SERVER, 'SELECT CONVERT(MAX(shift_started), SQL_VARCHAR) as shift_date,
SUM(CASE WHEN customer = ''35''
THEN net
ELSE 0
END) AS intercompany_qty,
SUM(CASE WHEN customer <> ''35''
THEN net
ELSE 0
END) AS outside_qty
FROM salestkt
WHERE (void is null or void = false) and
incoming_material = false and
shift_started = (SELECT MAX(STK.shift_started) as shift
FROM salestkt AS STK
WHERE EXISTS(SELECT *
FROM SHFTDATE AS SD
WHERE SD.shift_started is not Null and
SD.shift_started = STK.shift_started))')

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-06-22 : 10:56:07
you can add a section at last to check record count and if its 0 add the default values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2010-06-22 : 13:29:56
That is what I had in mind. Thank you.
Go to Top of Page
   

- Advertisement -