create proc wsmsp_Statistic_StatisticByQuestionState
@productId uniqueidentifier,
@moduleId uniqueidentifier
如上面存储过程,
现需根据 @productId 去组合查询条件
若 @productId  为初始值 则查询所有
请问 这个初始值该怎么传入 存储过程,初始值应该为什么?

解决方案 »

  1.   

    应该可以在存储过程中默认为NULL,如果要查询所有,可以让外面调用的程序对@productId不传值,存储过程中判断到@productId为NULL时查询所有,否则按条件查询
      

  2.   

    uniqueidentifier
    全局唯一标识符 (GUID)。注释
    uniqueidentifier 数据类型的列或局部变量可用两种方法初始化为一个值: 使用 NEWID 函数。
    将字符串常量转换为如下形式(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字)。例如,6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 uniqueidentifier 值。 
    比较运算符可与 uniqueidentifier 值一起使用。然而,排列并非通过比较两个值的位模式来实现。允许对 uniqueidentifier 值执行的操作只有比较 (=, <>, <, >, <=, >=) 和检查 NULL(IS NULL 和 IS NOT NULL)。不允许使用其它算术运算符。所有的列约束及属性(IDENTITY 除外)均允许用于 uniqueidentifier 数据类型。 
      

  3.   


    用null判断没有效果,我用'00000000-0000-0000-000000000000'判断 
    报错从数据类型 varchar 转换为 uniqueidentifier 时出错。
      

  4.   

    exec wsmsp_Statistic_StatisticByQuestionState 第一个值,第二个值
    exec wsmsp_Statistic_StatisticByQuestionState '6F9619FF-8B86-D011-B42D-00C04FC964FF',null
    exec wsmsp_Statistic_StatisticByQuestionState null,null
      

  5.   

    create table #t (productId uniqueidentifier,moduleId uniqueidentifier,cn varchar(100))
    go
    create proc wsmsp_Statistic_StatisticByQuestionState
        @productId uniqueidentifier='6F9619FF-8B86-D011-B42D-00C04FC964FF',
        @moduleId uniqueidentifier
    as
    begin
    select * from #t where productId = @productId and moduleId=@moduleId
    end
    go
    exec wsmsp_Statistic_StatisticByQuestionState default,'DD9619FF-8B86-D011-B42D-00C04FC964FF'drop table #t
    godrop proc wsmsp_Statistic_StatisticByQuestionState
    gouniqueidentifier 数据类型的列或局部变量可通过以下方式初始化为一个值:使用 NEWID 函数。
    通过从 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 形式的字符串常量进行转换,其中,每个 x 都是 0-9 或 a-f 范围内的十六进制数字。例如,6F9619FF-8B86-D011-B42D-00C04FC964FF 为有效的 uniqueidentifier 值。
    比较运算符可与 uniqueidentifier 值一起使用。不过,排序不是通过比较两个值的位模式来实现的。可针对 uniqueidentifier 值执行的运算只有比较运算(=、<>、<、>、<=、>=)以及检查是否为 NULL(IS NULL 和 IS NOT NULL)。不能使用其他算术运算符。除 IDENTITY 之外的所有列约束和属性均可对 uniqueidentifier 数据类型使用。