Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 如何在SQL Server 2008中使用if或case_Sql Server 2008 - Fatal编程技术网

Sql server 2008 如何在SQL Server 2008中使用if或case

Sql server 2008 如何在SQL Server 2008中使用if或case,sql-server-2008,Sql Server 2008,我是SQL Server新手,正在尝试通过名称和类型(CD/DVD)搜索一些CD或DVD 如果问题没有解决,我想显示我库存中的CD或DVD数量 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

我是SQL Server新手,正在尝试通过名称和类型(CD/DVD)搜索一些CD或DVD

如果问题没有解决,我想显示我库存中的CD或DVD数量

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

但是第一步(如果DVD…)不起作用。

您所说的“不起作用”是什么意思?结果是:命令成功完成。但结果显示为0。当我把If语句(If(@typ-like'DVD')放出来时,它显示了10个以上的结果…检查这个链接
DECLARE@typ-VARCHAR
将声明一个变量
VARCHAR(1)
意思是
DVD
将被截断为
D
。声明它的长度。也可以使用
=
而不是像
那样使用
,因为您没有使用任何通配符。