bondzh

SQL Server 2008 - Stored Procedures 简单范例

输入产品代码,输出产品信息 :


USE [ProductDatabase]

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
    usp_Prod_SEL_Product :  选取产品

    Input Parameters: 输入参数
        @ProdKey        varchar(10)    -- Product Key
    Output: 输出   Recordset

    Usage:    使用方法

        exec [usp_Prod_SEL_Product]    @ProdKey

    Sample:    范例 --选取下面一行代码,执行

        exec [usp_Prod_SEL_Product]    '1234567890'
*/

Create procedure [usp_Prod_SEL_Product] (
    @ProdKey         varchar(10)
) as
begin
    set nocount on
    set transaction isolation level read uncommitted

    select         p.[key], p.[description]
    from          [Product] p
    where        p.[key] like '%' + @ProdKey+ '%' --类似输入的key
    order by    p.[description]
end

------------------------------------------
其他:
1) 如果是空:换成其他字符: isnull([description], 'N/A') as ProductDescription
2) 把字符表示格式转换:            substring([key],1,5) + '-' + substring([key],6,5) as ProductKey, -- '1234567890' >> '12345-67890'
3) 选择只显示在某个特定日期的值:
case
    when GetDate() between  dateBegin and dateEnd then
        [key]
    else
        null
end as ProductKey,



posted on 2008-06-19 17:00 DestinyController 阅读(304) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。