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
 insert problem

Author  Topic 

Roald
Starting Member

3 Posts

Posted - 2011-10-13 : 09:00:31
I have the following sql query. The query is supposed to insert the correlation and an id. For the id i want to use the variable kolom witch is used to calculate it, this is the variable @collomnaam2.

Basicly this is what i want as output:

Correlatie id
0,834511836068776 v1
1 v3
0,756773018571784 v17
and so on.

Basicly my question is how do i rewrite my statement to get the following output while keeping my query dynamic?

Thanks in advance,

Gr Roald


--------------------------------------------------------------------------------------------------------------------
-- Variabelen die zelf ingevult moeten worden:
Declare @database varchar(40)
set @database = '11_23_020_Intrum_Justitia' -- welke database wil je gebruiken
Declare @orgineletabel varchar(40)
set @orgineletabel = 'EXTRACT$'-- welke tabel wil je gebruiken
Declare @dbtabel varchar(40)
set @dbtabel = 'Correlatie' -- naar welke tabel wil je de gegevens wegschrijven
Declare @collomnaam varchar(40)
set @collomnaam = 'v3' -- welke kolom wil je als basis gebruiken voor de berekening
Declare @kolomsegment varchar(40)
set @kolomsegment = '0' -- in welke kolom het segment staat.
Declare @segment varchar(40)
set @segment = '0' -- welke segment wil je berekenen, als je geen segment wil berekenen kan je @kolomsegment en @segment beide op '0' zetten.
---------------------------------------------------------------------------------------------------------------------
Declare @int int
set @int = 1
Declare @collomnaam2 varchar(40)
set @collomnaam2 = 'v'+cast(@int AS VARCHAR(40))


while (@int < 100)-- zolang V-nummer kleiner is dan 100 statement uitvoeren je kan gewoon het nummer veranderen aan de hand van het hoogste V nummer

begin
Declare @statemant varchar(2000)
set @statemant =
'insert into ['+@database+ '].[dbo].['+@dbtabel+'](Correlatie, id)
SELECT ['+@database+'].dbo.WeightedCorrelation('+@collomnaam+','+@collomnaam2+',1)
FROM ['+@database+'].[dbo].['+@orgineletabel+']
WHERE ' +@collomnaam2+ ' IS NOT NULL AND '+@collomnaam+' IS NOT NULL AND ' +@kolomsegment+' = '+@segment+''

Begin



execute(@statemant)
SET @int = @int + 1
Set @collomnaam2 = 'v'+cast(@int AS VARCHAR(40))

end
end

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-11-15 : 06:07:57
What does @statemant return? You need to post some sample data with expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -