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.
| Author |
Topic |
|
java148
Yak Posting Veteran
63 Posts |
Posted - 2011-11-22 : 15:59:06
|
how to write a query to caculate count ? if get data from select, add one to count, something like this:declare @count = 0;if(exists(select 1 from mytable where id='22')){ @count = @count + 1;} |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
java148
Yak Posting Veteran
63 Posts |
Posted - 2011-11-22 : 22:09:14
|
| Thanks. if I want to print ID out if that record is not found, how to do it ? write another select statement ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-22 : 23:45:12
|
quote: Originally posted by java148 Thanks. if I want to print ID out if that record is not found, how to do it ? write another select statement ?
hmm..what does that mean?if record is not found, which id you've to print?are you try to match records between two tables based on id?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-11-23 : 00:10:30
|
| i think you want thisDROP TABLE #tmpCREATE TABLE #tmp (id VARCHAR(max))INSERT INTO #tmp (id)SELECT 12UNION ALLSELECT 13UNION ALLSELECT 14UNION ALLSELECT 15UNION ALLSELECT 16DECLARE @ids VARCHAR(MAX)SET @ids='12,16,18,13'SELECT * FROM dbo.Split(@ids,',') AS t WHERE t.items NOT IN (SELECT id FROM #tmp )Get split function script from below ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return end |
 |
|
|
java148
Yak Posting Veteran
63 Posts |
Posted - 2011-11-23 : 10:29:37
|
what I want to do is like this:declare @count intset @count = 0;if(exists(select 1 from mytbl where id = '111')){ @count = @count + 1;}else { print 'id 111 is not found in mytbl';} |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-11-24 : 01:30:54
|
| If exists(select * from from yourtablewhere id = '111')print 'found'elseprint 'not found'MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|