0

I am new to SQL Server and I am trying to search some CDs or DVDs through their name and type(CD/DVD).

If they are not solved, I would like to show the amount CDs or DVDs, which I have in my stock.

DECLARE @typ VARCHAR
SET @name = 'Kil'
SET @typ = 'DVD'

IF (@typ like 'DVD')
begin
    select titul.name from titul 
    where titul_id in (
        select titul_id from dvd
        where titul_id in (
            select titul_id 
            from titul 
            where titul.name like @name+'%'
        )   
        and (dvd.soldDate is NULL) 
    )
    group by titul.name
end

But the first step (If DVD..) does not work.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • what do you mean by "*does not work*"? – Cristian Lupascu May 19 '13 at 10:21
  • the result is: Command(s) completed successfully. But 0 results is shown. When I put the If statement (IF (@typ like 'DVD')) out, it shows more than 10 results ... – user2350401 May 19 '13 at 10:52
  • Check this link http://stackoverflow.com/questions/1122557/sql-server-2008-case-if-statements-in-select-clause http://stackoverflow.com/questions/7872242/if-loop-in-then-statement-of-case-statement-in-sql-server-2008 – Ajay May 19 '13 at 10:58
  • 2
    `DECLARE @typ VARCHAR` will declare a variable of `varchar(1)` meaning `DVD` will get truncated to `D`. Declare it with a length. And also use `=` instead of `LIKE` as you aren't using any wildcards. – Martin Smith May 19 '13 at 12:08

0 Answers0