T-SQL | How to SET Value dynamically to Variables in While Loop
I have 12 Variables and all of them have the same SELECT Statement exept for 1 Param. I somehow cant figure out how to so it dynamically.
Here is an example of what I have:
// this here should be dynamic
SET @Var1 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '1')
SET @Var2 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '2')
SET @Var3 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '3')
SET @Var4 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '4')
SET @Var5 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '5')
SET @Var6 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '6')
SET @Var7 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '7')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '8')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '9')
SET @Var10 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '10')
SET @Var11 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '11')
SET @Var12 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '12')
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
VALUES (@Var1, @Var2, @Var3, @Var4, @Var5, @Var6, @Var7, @Var8, @Var9, @Var10, @Var11, @Var12)
The Table Cols are restricted (no permission to change the table) so I need to assign it like this.
sql tsql
add a comment |
I have 12 Variables and all of them have the same SELECT Statement exept for 1 Param. I somehow cant figure out how to so it dynamically.
Here is an example of what I have:
// this here should be dynamic
SET @Var1 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '1')
SET @Var2 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '2')
SET @Var3 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '3')
SET @Var4 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '4')
SET @Var5 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '5')
SET @Var6 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '6')
SET @Var7 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '7')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '8')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '9')
SET @Var10 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '10')
SET @Var11 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '11')
SET @Var12 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '12')
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
VALUES (@Var1, @Var2, @Var3, @Var4, @Var5, @Var6, @Var7, @Var8, @Var9, @Var10, @Var11, @Var12)
The Table Cols are restricted (no permission to change the table) so I need to assign it like this.
sql tsql
add a comment |
I have 12 Variables and all of them have the same SELECT Statement exept for 1 Param. I somehow cant figure out how to so it dynamically.
Here is an example of what I have:
// this here should be dynamic
SET @Var1 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '1')
SET @Var2 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '2')
SET @Var3 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '3')
SET @Var4 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '4')
SET @Var5 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '5')
SET @Var6 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '6')
SET @Var7 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '7')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '8')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '9')
SET @Var10 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '10')
SET @Var11 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '11')
SET @Var12 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '12')
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
VALUES (@Var1, @Var2, @Var3, @Var4, @Var5, @Var6, @Var7, @Var8, @Var9, @Var10, @Var11, @Var12)
The Table Cols are restricted (no permission to change the table) so I need to assign it like this.
sql tsql
I have 12 Variables and all of them have the same SELECT Statement exept for 1 Param. I somehow cant figure out how to so it dynamically.
Here is an example of what I have:
// this here should be dynamic
SET @Var1 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '1')
SET @Var2 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '2')
SET @Var3 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '3')
SET @Var4 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '4')
SET @Var5 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '5')
SET @Var6 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '6')
SET @Var7 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '7')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '8')
SET @Var9 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '9')
SET @Var10 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '10')
SET @Var11 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '11')
SET @Var12 = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = '12')
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
VALUES (@Var1, @Var2, @Var3, @Var4, @Var5, @Var6, @Var7, @Var8, @Var9, @Var10, @Var11, @Var12)
The Table Cols are restricted (no permission to change the table) so I need to assign it like this.
sql tsql
sql tsql
asked Nov 14 '18 at 9:37
user9168386
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
This looks like it should be a PIVOT:
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM (select MonthCol,COUNT(*) Cnt from #TempTable group by MonthCol) t
PIVOT (SUM(Cnt) FOR MonthCol IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) u
I believe we have to "pre-aggregate" the data in a subquery because COUNT(*) isn't allowed in the PIVOT clause1. Note that each MonthCol will have a single Cnt value so we could use any aggregate in the PIVOT that returns a single input unaltered - SUM, MIN or MAX. I arbitrarily picked SUM above.
1You have to supply specific column(s) to the aggregate.
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
add a comment |
I think you don't need to use variable here :
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT SUM(CASE WHEN MonthCol = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN MonthCol = 2 THEN 1 ELSE 0 END),
. . .
SUM(CASE WHEN MonthCol = 12 THEN 1 ELSE 0 END)
FROM #TempTable t;
However, here conditional aggregation is enough as there is only 12 months. So, dynamic approach is not needed.
add a comment |
well this is what you can try here:
Declare a table variable with 2 columns
declare @mytable table(
VariableName varchar(255),
VariableValue int
)
Now Declare 2 more variables, one to set value and second to iterate through loop
Declare @value int;
Declare @month int = 1;
Now do the while loop like this:
while (@month <= 12 ) --since there are only 12 months
begin
set @value = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = cast(@month as varchar));
insert into @mytable values ('Month'+cast (@month as varchar), value );
set @month = @month +1;
end
Now you'll have your DataSet in table form which you can access by using the simple query:
VariableName VariableValue
----------- --------------
Month1 23
Month2 19
Month3 66
Month4 12
Month5 05
Month6 04
Month7 63
Month8 76
Month9 21
Month10 23
Month11 63
Month12 12
Select * from @mytable where VariableName = 'Month1'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297010%2ft-sql-how-to-set-value-dynamically-to-variables-in-while-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This looks like it should be a PIVOT:
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM (select MonthCol,COUNT(*) Cnt from #TempTable group by MonthCol) t
PIVOT (SUM(Cnt) FOR MonthCol IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) u
I believe we have to "pre-aggregate" the data in a subquery because COUNT(*) isn't allowed in the PIVOT clause1. Note that each MonthCol will have a single Cnt value so we could use any aggregate in the PIVOT that returns a single input unaltered - SUM, MIN or MAX. I arbitrarily picked SUM above.
1You have to supply specific column(s) to the aggregate.
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
add a comment |
This looks like it should be a PIVOT:
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM (select MonthCol,COUNT(*) Cnt from #TempTable group by MonthCol) t
PIVOT (SUM(Cnt) FOR MonthCol IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) u
I believe we have to "pre-aggregate" the data in a subquery because COUNT(*) isn't allowed in the PIVOT clause1. Note that each MonthCol will have a single Cnt value so we could use any aggregate in the PIVOT that returns a single input unaltered - SUM, MIN or MAX. I arbitrarily picked SUM above.
1You have to supply specific column(s) to the aggregate.
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
add a comment |
This looks like it should be a PIVOT:
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM (select MonthCol,COUNT(*) Cnt from #TempTable group by MonthCol) t
PIVOT (SUM(Cnt) FOR MonthCol IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) u
I believe we have to "pre-aggregate" the data in a subquery because COUNT(*) isn't allowed in the PIVOT clause1. Note that each MonthCol will have a single Cnt value so we could use any aggregate in the PIVOT that returns a single input unaltered - SUM, MIN or MAX. I arbitrarily picked SUM above.
1You have to supply specific column(s) to the aggregate.
This looks like it should be a PIVOT:
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM (select MonthCol,COUNT(*) Cnt from #TempTable group by MonthCol) t
PIVOT (SUM(Cnt) FOR MonthCol IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) u
I believe we have to "pre-aggregate" the data in a subquery because COUNT(*) isn't allowed in the PIVOT clause1. Note that each MonthCol will have a single Cnt value so we could use any aggregate in the PIVOT that returns a single input unaltered - SUM, MIN or MAX. I arbitrarily picked SUM above.
1You have to supply specific column(s) to the aggregate.
answered Nov 14 '18 at 10:43
Damien_The_UnbelieverDamien_The_Unbeliever
194k17248335
194k17248335
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
add a comment |
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
Damn. Well this is what I needed. The other solutions mentioned are working too. But this is what I really asked for. Thanks. EDIT: Thanks for explaining how it works.
– user9168386
Nov 14 '18 at 14:53
add a comment |
I think you don't need to use variable here :
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT SUM(CASE WHEN MonthCol = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN MonthCol = 2 THEN 1 ELSE 0 END),
. . .
SUM(CASE WHEN MonthCol = 12 THEN 1 ELSE 0 END)
FROM #TempTable t;
However, here conditional aggregation is enough as there is only 12 months. So, dynamic approach is not needed.
add a comment |
I think you don't need to use variable here :
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT SUM(CASE WHEN MonthCol = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN MonthCol = 2 THEN 1 ELSE 0 END),
. . .
SUM(CASE WHEN MonthCol = 12 THEN 1 ELSE 0 END)
FROM #TempTable t;
However, here conditional aggregation is enough as there is only 12 months. So, dynamic approach is not needed.
add a comment |
I think you don't need to use variable here :
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT SUM(CASE WHEN MonthCol = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN MonthCol = 2 THEN 1 ELSE 0 END),
. . .
SUM(CASE WHEN MonthCol = 12 THEN 1 ELSE 0 END)
FROM #TempTable t;
However, here conditional aggregation is enough as there is only 12 months. So, dynamic approach is not needed.
I think you don't need to use variable here :
INSERT INTO VarTable (Var1, Var2, Var3, Var4, Var5, Var6, Var7, Var8, Var9, Var10, Var11, Var12)
SELECT SUM(CASE WHEN MonthCol = 1 THEN 1 ELSE 0 END),
SUM(CASE WHEN MonthCol = 2 THEN 1 ELSE 0 END),
. . .
SUM(CASE WHEN MonthCol = 12 THEN 1 ELSE 0 END)
FROM #TempTable t;
However, here conditional aggregation is enough as there is only 12 months. So, dynamic approach is not needed.
answered Nov 14 '18 at 9:41
Yogesh SharmaYogesh Sharma
30.2k51436
30.2k51436
add a comment |
add a comment |
well this is what you can try here:
Declare a table variable with 2 columns
declare @mytable table(
VariableName varchar(255),
VariableValue int
)
Now Declare 2 more variables, one to set value and second to iterate through loop
Declare @value int;
Declare @month int = 1;
Now do the while loop like this:
while (@month <= 12 ) --since there are only 12 months
begin
set @value = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = cast(@month as varchar));
insert into @mytable values ('Month'+cast (@month as varchar), value );
set @month = @month +1;
end
Now you'll have your DataSet in table form which you can access by using the simple query:
VariableName VariableValue
----------- --------------
Month1 23
Month2 19
Month3 66
Month4 12
Month5 05
Month6 04
Month7 63
Month8 76
Month9 21
Month10 23
Month11 63
Month12 12
Select * from @mytable where VariableName = 'Month1'
add a comment |
well this is what you can try here:
Declare a table variable with 2 columns
declare @mytable table(
VariableName varchar(255),
VariableValue int
)
Now Declare 2 more variables, one to set value and second to iterate through loop
Declare @value int;
Declare @month int = 1;
Now do the while loop like this:
while (@month <= 12 ) --since there are only 12 months
begin
set @value = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = cast(@month as varchar));
insert into @mytable values ('Month'+cast (@month as varchar), value );
set @month = @month +1;
end
Now you'll have your DataSet in table form which you can access by using the simple query:
VariableName VariableValue
----------- --------------
Month1 23
Month2 19
Month3 66
Month4 12
Month5 05
Month6 04
Month7 63
Month8 76
Month9 21
Month10 23
Month11 63
Month12 12
Select * from @mytable where VariableName = 'Month1'
add a comment |
well this is what you can try here:
Declare a table variable with 2 columns
declare @mytable table(
VariableName varchar(255),
VariableValue int
)
Now Declare 2 more variables, one to set value and second to iterate through loop
Declare @value int;
Declare @month int = 1;
Now do the while loop like this:
while (@month <= 12 ) --since there are only 12 months
begin
set @value = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = cast(@month as varchar));
insert into @mytable values ('Month'+cast (@month as varchar), value );
set @month = @month +1;
end
Now you'll have your DataSet in table form which you can access by using the simple query:
VariableName VariableValue
----------- --------------
Month1 23
Month2 19
Month3 66
Month4 12
Month5 05
Month6 04
Month7 63
Month8 76
Month9 21
Month10 23
Month11 63
Month12 12
Select * from @mytable where VariableName = 'Month1'
well this is what you can try here:
Declare a table variable with 2 columns
declare @mytable table(
VariableName varchar(255),
VariableValue int
)
Now Declare 2 more variables, one to set value and second to iterate through loop
Declare @value int;
Declare @month int = 1;
Now do the while loop like this:
while (@month <= 12 ) --since there are only 12 months
begin
set @value = (SELECT COUNT(*) FROM #TempTable WHERE MonthCol = cast(@month as varchar));
insert into @mytable values ('Month'+cast (@month as varchar), value );
set @month = @month +1;
end
Now you'll have your DataSet in table form which you can access by using the simple query:
VariableName VariableValue
----------- --------------
Month1 23
Month2 19
Month3 66
Month4 12
Month5 05
Month6 04
Month7 63
Month8 76
Month9 21
Month10 23
Month11 63
Month12 12
Select * from @mytable where VariableName = 'Month1'
edited Nov 14 '18 at 10:49
answered Nov 14 '18 at 10:39
ARr0wARr0w
1,060728
1,060728
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297010%2ft-sql-how-to-set-value-dynamically-to-variables-in-while-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown