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
 table variable comapre

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-02-27 : 08:32:41

i insert some values from a table in table variable
the table vaible have one column

insert into @vtbl (vid )
select id FROM abc ;

then after this i need to loop through
table variable.

the objective is to check there is second value in table vaiable and
this should be equal to first one .


please help ...

challenge everything

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-27 : 08:40:11
You want to find duplicates?
select vid, count(*) as cnt from @vtbl group by vid having count(*) > 1



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-02-27 : 08:47:10
no i need exact 2 same values

challenge everything
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 08:52:03
TO find rows that have *exactly* one duplicate [vid]
select vid as cnt from @vtbl group by vid having count(*) = 2


To Find rows that do not.
select vid as cnt from @vtbl group by vid having count(*) <> 2


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2012-02-27 : 08:54:34
i need to match first value with second , in case i have only twovalues at a time in table variable

challenge everything
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-27 : 09:01:42
what you are saying makes no sense (given the information you provided)

1) You have a table variable with only one column ([vid])
2) you want to find [vid] values where there is exactly one duplicate [vid] values.

The sql we posted does that.

SELECT [vid] FROM @vtbl GROUP BY [vid] HAVING COUNT([vid]) = 2

Shows you the [vid] entries where there is exactly 1 duplicate vid.

I suspect what you want to do is actually check other columns in some other table but you haven't told us about them so we can't help you.

read what you write and try and think about how it looks to someone who has no idea of your system or what you want except what you wrote.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -