while loop select in SQL Server
I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.
I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like
declare @serial int
select @serial = select serial from T1
while @serial
begin
select count(*) from T2 where Serial = @serial
end
But I get an error:
Incorrect syntax near the keyword 'select'
How to do it? Thanks.
sql
add a comment |
I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.
I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like
declare @serial int
select @serial = select serial from T1
while @serial
begin
select count(*) from T2 where Serial = @serial
end
But I get an error:
Incorrect syntax near the keyword 'select'
How to do it? Thanks.
sql
Can't you justSELECT COUNT(*) FROM T2 WHERE Serial = @Serial?
– Sami
Nov 15 '18 at 18:58
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24
add a comment |
I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.
I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like
declare @serial int
select @serial = select serial from T1
while @serial
begin
select count(*) from T2 where Serial = @serial
end
But I get an error:
Incorrect syntax near the keyword 'select'
How to do it? Thanks.
sql
I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.
I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like
declare @serial int
select @serial = select serial from T1
while @serial
begin
select count(*) from T2 where Serial = @serial
end
But I get an error:
Incorrect syntax near the keyword 'select'
How to do it? Thanks.
sql
sql
edited Nov 15 '18 at 21:42
marc_s
580k13011191266
580k13011191266
asked Nov 15 '18 at 18:56
Chuck PedroChuck Pedro
8618
8618
Can't you justSELECT COUNT(*) FROM T2 WHERE Serial = @Serial?
– Sami
Nov 15 '18 at 18:58
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24
add a comment |
Can't you justSELECT COUNT(*) FROM T2 WHERE Serial = @Serial?
– Sami
Nov 15 '18 at 18:58
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24
Can't you just
SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?– Sami
Nov 15 '18 at 18:58
Can't you just
SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?– Sami
Nov 15 '18 at 18:58
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24
add a comment |
5 Answers
5
active
oldest
votes
First of all, doing:
select @serial = select serial from T1
while @serial
…
Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).
What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:
SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;
add a comment |
Instead of while loop, just join the tables and use an aggregate.
Something like:
select a.serial, count(a.serial) from t2 a
inner join t1 b on b.serial = a.serial
group by a.serial
Without sample data I can't test it out for you, but that will perform a lot better for you.
add a comment |
Simply
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial
There is no need to declare a variable or to use a loop.
If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial;
Simple Demo
add a comment |
Yes it is a syntax error
select @serial = select serial from T1; -- is wrong it should as written below
select @serial = serial from T1 ;
This will select the 1st value from Table T1.
This will remove the error but the query written will not yeald the required output.
You need to loop through table T1 and for each value of T1 search in T2.
See if the below simple query helps
select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
group by T1.serial.
add a comment |
Seems like you could just do this in one quick statement, rather than a loop.
SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
FROM T1
INNER JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T2.Serial
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%2f53326191%2fwhile-loop-select-in-sql-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, doing:
select @serial = select serial from T1
while @serial
…
Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).
What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:
SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;
add a comment |
First of all, doing:
select @serial = select serial from T1
while @serial
…
Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).
What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:
SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;
add a comment |
First of all, doing:
select @serial = select serial from T1
while @serial
…
Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).
What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:
SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;
First of all, doing:
select @serial = select serial from T1
while @serial
…
Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).
What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:
SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;
answered Nov 15 '18 at 19:03
LamakLamak
59.3k87998
59.3k87998
add a comment |
add a comment |
Instead of while loop, just join the tables and use an aggregate.
Something like:
select a.serial, count(a.serial) from t2 a
inner join t1 b on b.serial = a.serial
group by a.serial
Without sample data I can't test it out for you, but that will perform a lot better for you.
add a comment |
Instead of while loop, just join the tables and use an aggregate.
Something like:
select a.serial, count(a.serial) from t2 a
inner join t1 b on b.serial = a.serial
group by a.serial
Without sample data I can't test it out for you, but that will perform a lot better for you.
add a comment |
Instead of while loop, just join the tables and use an aggregate.
Something like:
select a.serial, count(a.serial) from t2 a
inner join t1 b on b.serial = a.serial
group by a.serial
Without sample data I can't test it out for you, but that will perform a lot better for you.
Instead of while loop, just join the tables and use an aggregate.
Something like:
select a.serial, count(a.serial) from t2 a
inner join t1 b on b.serial = a.serial
group by a.serial
Without sample data I can't test it out for you, but that will perform a lot better for you.
answered Nov 15 '18 at 19:02
Tim MylottTim Mylott
1,20319
1,20319
add a comment |
add a comment |
Simply
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial
There is no need to declare a variable or to use a loop.
If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial;
Simple Demo
add a comment |
Simply
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial
There is no need to declare a variable or to use a loop.
If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial;
Simple Demo
add a comment |
Simply
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial
There is no need to declare a variable or to use a loop.
If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial;
Simple Demo
Simply
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial
There is no need to declare a variable or to use a loop.
If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead
SELECT T1.Serial,
COUNT(T2.Serial) AppearsInT2
FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T1.Serial;
Simple Demo
edited Nov 15 '18 at 19:09
answered Nov 15 '18 at 19:03
SamiSami
8,98831242
8,98831242
add a comment |
add a comment |
Yes it is a syntax error
select @serial = select serial from T1; -- is wrong it should as written below
select @serial = serial from T1 ;
This will select the 1st value from Table T1.
This will remove the error but the query written will not yeald the required output.
You need to loop through table T1 and for each value of T1 search in T2.
See if the below simple query helps
select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
group by T1.serial.
add a comment |
Yes it is a syntax error
select @serial = select serial from T1; -- is wrong it should as written below
select @serial = serial from T1 ;
This will select the 1st value from Table T1.
This will remove the error but the query written will not yeald the required output.
You need to loop through table T1 and for each value of T1 search in T2.
See if the below simple query helps
select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
group by T1.serial.
add a comment |
Yes it is a syntax error
select @serial = select serial from T1; -- is wrong it should as written below
select @serial = serial from T1 ;
This will select the 1st value from Table T1.
This will remove the error but the query written will not yeald the required output.
You need to loop through table T1 and for each value of T1 search in T2.
See if the below simple query helps
select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
group by T1.serial.
Yes it is a syntax error
select @serial = select serial from T1; -- is wrong it should as written below
select @serial = serial from T1 ;
This will select the 1st value from Table T1.
This will remove the error but the query written will not yeald the required output.
You need to loop through table T1 and for each value of T1 search in T2.
See if the below simple query helps
select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
group by T1.serial.
answered Nov 15 '18 at 19:15
RoshanRoshan
212
212
add a comment |
add a comment |
Seems like you could just do this in one quick statement, rather than a loop.
SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
FROM T1
INNER JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T2.Serial
add a comment |
Seems like you could just do this in one quick statement, rather than a loop.
SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
FROM T1
INNER JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T2.Serial
add a comment |
Seems like you could just do this in one quick statement, rather than a loop.
SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
FROM T1
INNER JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T2.Serial
Seems like you could just do this in one quick statement, rather than a loop.
SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
FROM T1
INNER JOIN T2 ON T1.Serial = T2.Serial
GROUP BY T2.Serial
edited Nov 15 '18 at 19:30
Sami
8,98831242
8,98831242
answered Nov 15 '18 at 19:29
Brian SingletonBrian Singleton
112
112
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%2f53326191%2fwhile-loop-select-in-sql-server%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
Can't you just
SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?– Sami
Nov 15 '18 at 18:58
No, some serials are repeated. I need to find which ones are repeated in T2.
– Chuck Pedro
Nov 15 '18 at 18:59
Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?
– Tim Mylott
Nov 15 '18 at 19:24