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
 Chage to select into

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-05-07 : 16:53:24
I need the following query changed into a "Select * into". I've been through every permutation i can think of and i keep getting errors. One error is on the persontypeid=persontypeid as well as the personid=personid. Not sure how to alias those, but aside from that, it's an issue of the wrong syntax being used in general, elswhere.


SELECT *,
CASE WHEN epd.ComplainantVictim = 1 THEN 'Victim' ELSE ptd.Description END AS PersonType
FROM PersonData pd, PersonTypeDesc ptd, CFSExtraPersonData epd
WHERE --pd.FormID = 779457
pd.FormTypeID IN (10000,10001,10010)
AND pd.PersonTypeID = ptd.PersonTypeID
AND pd.PersonID = epd.PersonID
AND (ptd.Description IN ('Subject','Victim') OR epd.ComplainantVictim = 1)
AND pd.LastName IS NOT NULL
AND ptd.description='Subject'
ORDER BY PersonType DESC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-07 : 17:06:27
Replace * with actual column names. Particularly if there are multiple columns with same name coming from different tables give aliases to them to name them differently

SELECT actual column names here,
CASE WHEN epd.ComplainantVictim = 1 THEN 'Victim' ELSE ptd.Description END AS PersonType
INTO YourTableName
FROM PersonData pd, PersonTypeDesc ptd, CFSExtraPersonData epd
WHERE --pd.FormID = 779457
pd.FormTypeID IN (10000,10001,10010)
AND pd.PersonTypeID = ptd.PersonTypeID
AND pd.PersonID = epd.PersonID
AND (ptd.Description IN ('Subject','Victim') OR epd.ComplainantVictim = 1)
AND pd.LastName IS NOT NULL
AND ptd.description='Subject'
ORDER BY PersonType DESC


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

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-05-08 : 08:51:11
Ugh. I knew i had done this before. I wasn't looking at the bulk of the query as the "where statement". Thanks for the nudge.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 11:18:10
welcome

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

Go to Top of Page
   

- Advertisement -