I'm kind of new to sql and I'm wondering if there is a more efficient way to accomplish the following code:
DECLARE @csv VARCHAR(255)
DECLARE @ValueId int
--Doc Type
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='DocType')
SELECT @csv = '~DT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--PurposeTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='PurposeTypeAlias')
SELECT @csv = @csv + '~PT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--OccupancyTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='OccupancyTypeAlias')
SELECT @csv = @csv + '~OT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--PropertyTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='PropertyTypeAlias')
SELECT @csv = @csv + '~PRT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--LTV
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='LTV')
SELECT @csv = @csv + '~LTV~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CLTV
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CLTV')
SELECT @csv = @csv + '~CLTV~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--LoanAmount
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='LoanAmount')
SELECT @csv = @csv + '~LA~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CashOut
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CashOut')
SELECT @csv = @csv + '~CO~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CashOutPct
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CashOutPct')
SELECT @csv = @csv + '~COP~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CreditScore
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CreditScore')
SELECT @csv = @csv + '~CS~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--AUSType
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='AUSType')
SELECT @csv = @csv + '~AUS~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
RETURN @csv
I would greatly appreciate the help guys.
DECLARE @csv VARCHAR(255)
DECLARE @ValueId int
--Doc Type
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='DocType')
SELECT @csv = '~DT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--PurposeTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='PurposeTypeAlias')
SELECT @csv = @csv + '~PT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--OccupancyTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='OccupancyTypeAlias')
SELECT @csv = @csv + '~OT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--PropertyTypeAlias
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='PropertyTypeAlias')
SELECT @csv = @csv + '~PRT~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--LTV
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='LTV')
SELECT @csv = @csv + '~LTV~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CLTV
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CLTV')
SELECT @csv = @csv + '~CLTV~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--LoanAmount
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='LoanAmount')
SELECT @csv = @csv + '~LA~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CashOut
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CashOut')
SELECT @csv = @csv + '~CO~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CashOutPct
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CashOutPct')
SELECT @csv = @csv + '~COP~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--CreditScore
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='CreditScore')
SELECT @csv = @csv + '~CS~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
--AUSType
SELECT @ValueId = NULL
SELECT @ValueId = (SELECT valueid from ps_value where categoryid=@categoryid and guidelineid=@guidelineid and fieldname='AUSType')
SELECT @csv = @csv + '~AUS~'
select @csv = coalesce(@csv+',','') + fielddata
from ps_valuedata
where categoryid=@categoryid and guidelineid=@guidelineid and valueid=@ValueId
RETURN @csv
I would greatly appreciate the help guys.