下面是一些常用例子,能解决类似问题
A. Use a simple procedure with a complex SELECT
This stored procedure returns all authors (first and last names supplied), their titles, and their publishers from a four-table join. This stored procedure does not use any parameters.USE pubs
IF EXISTS (SELECT name FROM sysobjects 
         WHERE name = 'au_info_all' AND type = 'P')
   DROP PROCEDURE au_info_all
GO
CREATE PROCEDURE au_info_all
AS
SELECT au_lname, au_fname, title, pub_name
   FROM authors a INNER JOIN titleauthor ta
      ON a.au_id = ta.au_id INNER JOIN titles t
      ON t.title_id = ta.title_id INNER JOIN publishers p
      ON t.pub_id = p.pub_id
GOThe au_info_all stored procedure can be executed in these ways:EXECUTE au_info_all
-- Or
EXEC au_info_allOr, if this procedure is the first statement within the batch:au_info_allB. Use a simple procedure with parameters 
This stored procedure returns only the specified authors (first and last names supplied), their titles, and their publishers from a four-table join. This stored procedure accepts exact matches for the parameters passed.USE pubs
IF EXISTS (SELECT name FROM sysobjects 
         WHERE name = 'au_info' AND type = 'P')
   DROP PROCEDURE au_info
GO
USE pubs
GO
CREATE PROCEDURE au_info 
   @lastname varchar(40), 
   @firstname varchar(20) 
AS 
SELECT au_lname, au_fname, title, pub_name
   FROM authors a INNER JOIN titleauthor ta
      ON a.au_id = ta.au_id INNER JOIN titles t
      ON t.title_id = ta.title_id INNER JOIN publishers p
      ON t.pub_id = p.pub_id
   WHERE  au_fname = @firstname
      AND au_lname = @lastname
GOThe au_info stored procedure can be executed in these ways:EXECUTE au_info 'Dull', 'Ann'
-- Or
EXECUTE au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
EXECUTE au_info @firstname = 'Ann', @lastname = 'Dull'
-- Or
EXEC au_info 'Dull', 'Ann'
-- Or
EXEC au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
EXEC au_info @firstname = 'Ann', @lastname = 'Dull'Or, if this procedure is the first statement within the batch:au_info 'Dull', 'Ann'
-- Or
au_info @lastname = 'Dull', @firstname = 'Ann'
-- Or
au_info @firstname = 'Ann', @lastname = 'Dull'C. Use a simple procedure with wildcard parameters 
This stored procedure returns only the specified authors (first and last names supplied), their titles, and their publishers from a four-table join. This stored procedure pattern matches the parameters passed or, if not supplied, uses the preset defaults.USE pubs
IF EXISTS (SELECT name FROM sysobjects 
      WHERE name = 'au_info2' AND type = 'P')
   DROP PROCEDURE au_info2
GO
USE pubs
GO
CREATE PROCEDURE au_info2
   @lastname varchar(30) = 'D%',
   @firstname varchar(18) = '%'
AS 
SELECT au_lname, au_fname, title, pub_name
FROM authors a INNER JOIN titleauthor ta
   ON a.au_id = ta.au_id INNER JOIN titles t
   ON t.title_id = ta.title_id INNER JOIN publishers p
   ON t.pub_id = p.pub_id
WHERE au_fname LIKE @firstname
   AND au_lname LIKE @lastname
GOThe au_info2 stored procedure can be executed in many combinations. Only a few combinations are shown here:EXECUTE au_info2
-- Or
EXECUTE au_info2 'Wh%'
-- Or
EXECUTE au_info2 @firstname = 'A%'
-- Or
EXECUTE au_info2 '[CK]ars[OE]n'
-- Or
EXECUTE au_info2 'Hunter', 'Sheryl'
-- Or
EXECUTE au_info2 'H%', 'S%'

解决方案 »

  1.   

    D. Use OUTPUT parameters
    OUTPUT parameters allow an external procedure, a batch, or more than one Transact-SQL statements to access a value set during the procedure execution. In this example, a stored procedure (titles_sum) is created and allows one optional input parameter and one output parameter.First, create the procedure:USE pubs
    GO
    IF EXISTS(SELECT name FROM sysobjects
          WHERE name = 'titles_sum' AND type = 'P')
       DROP PROCEDURE titles_sum
    GO
    USE pubs
    GO
    CREATE PROCEDURE titles_sum @@TITLE varchar(40) = '%', @@SUM money OUTPUT
    AS
    SELECT 'Title Name' = title
    FROM titles 
    WHERE title LIKE @@TITLE 
    SELECT @@SUM = SUM(price)
    FROM titles
    WHERE title LIKE @@TITLE
    GONext, use the OUTPUT parameter with control-of-flow language. Note  The OUTPUT variable must be defined during the table creation as well as during use of the variable.
    The parameter name and variable name do not have to match; however, the data type and parameter positioning must match (unless @@SUM = variable is used). DECLARE @@TOTALCOST money
    EXECUTE titles_sum 'The%', @@TOTALCOST OUTPUT
    IF @@TOTALCOST < 200 
    BEGIN
       PRINT ' '
       PRINT 'All of these titles can be purchased for less than $200.'
    END
    ELSE
       SELECT 'The total cost of these titles is $' 
             + RTRIM(CAST(@@TOTALCOST AS varchar(20)))Here is the result set:Title Name                                                               
    ------------------------------------------------------------------------ 
    The Busy Executive's Database Guide
    The Gourmet Microwave
    The Psychology of Computer Cooking(3 row(s) affected)Warning, null value eliminated from aggregate.
     
    All of these titles can be purchased for less than $200.E. Use an OUTPUT cursor parameter
    OUTPUT cursor parameters are used to pass a cursor that is local to a stored procedure back to the calling batch, stored procedure, or trigger.First, create the procedure that declares and then opens a cursor on the titles table:USE pubs
    IF EXISTS (SELECT name FROM sysobjects 
          WHERE name = 'titles_cursor' and type = 'P')
    DROP PROCEDURE titles_cursor
    GO
    CREATE PROCEDURE titles_cursor @titles_cursor CURSOR VARYING OUTPUT
    AS
    SET @titles_cursor = CURSOR
    FORWARD_ONLY STATIC FOR
    SELECT *
    FROM titlesOPEN @titles_cursor
    GONext, execute a batch that declares a local cursor variable, executes the procedure to assign the cursor to the local variable, and then fetches the rows from the cursor.USE pubs
    GO
    DECLARE @MyCursor CURSOR
    EXEC titles_cursor @titles_cursor = @MyCursor OUTPUT
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
       FETCH NEXT FROM @MyCursor
    END
    CLOSE @MyCursor
    DEALLOCATE @MyCursor
    GOF. Use the WITH RECOMPILE option
    The WITH RECOMPILE clause is helpful when the parameters supplied to the procedure will not be typical, and when a new execution plan should not be cached or stored in memory.USE pubs
    IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'titles_by_author' AND type = 'P')
       DROP PROCEDURE titles_by_author
    GO
    CREATE PROCEDURE titles_by_author @@LNAME_PATTERN varchar(30) = '%'
    WITH RECOMPILE
    AS
    SELECT RTRIM(au_fname) + ' ' + RTRIM(au_lname) AS 'Authors full name',
       title AS Title
    FROM authors a INNER JOIN titleauthor ta 
       ON a.au_id = ta.au_id INNER JOIN titles t
       ON ta.title_id = t.title_id
    WHERE au_lname LIKE @@LNAME_PATTERN
    GOG. Use the WITH ENCRYPTION option
    The WITH ENCRYPTION clause hides the text of a stored procedure from users. This example creates an encrypted procedure, uses the sp_helptext system stored procedure to get information on that encrypted procedure, and then attempts to get information on that procedure directly from the syscomments table.IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'encrypt_this' AND type = 'P')
       DROP PROCEDURE encrypt_this
    GO
    USE pubs
    GO
    CREATE PROCEDURE encrypt_this
    WITH ENCRYPTION
    AS
    SELECT * 
    FROM authors
    GOEXEC sp_helptext encrypt_thisHere is the result set:The object's comments have been encrypted.Next, select the identification number and text of the encrypted stored procedure contents.SELECT c.id, c.text 
    FROM syscomments c INNER JOIN sysobjects o
       ON c.id = o.id
    WHERE o.name = 'encrypt_this'Here is the result set:Note  The text column output is shown on a separate line. When executed, this information appears on the same line as the id column information.
    id         text                                                        
    ---------- ------------------------------------------------------------
    1413580074 ?????????????????????????????????e??????????????????????????????????????????????????????????????????????????(1 row(s) affected)H. Create a user-defined system stored procedure
    This example creates a procedure to display all the tables and their corresponding indexes with a table name beginning with the string emp. If not specified, this procedure returns all tables (and indexes) with a table name beginning with sys.IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'sp_showindexes' AND type = 'P')
       DROP PROCEDURE sp_showindexes
    GO
    USE master
    GO
    CREATE PROCEDURE sp_showindexes
       @@TABLE varchar(30) = 'sys%'
    AS 
    SELECT o.name AS TABLE_NAME,
       i.name AS INDEX_NAME, 
       indid AS INDEX_ID
    FROM sysindexes i INNER JOIN sysobjects o
       ON o.id = i.id 
    WHERE o.name LIKE @@TABLE
    GO         
    USE pubs
    EXEC sp_showindexes 'emp%'
    GOHere is the result set:TABLE_NAME       INDEX_NAME       INDEX_ID 
    ---------------- ---------------- ----------------
    employee         employee_ind     1
    employee         PK_emp_id        2(2 row(s) affected)I. Use deferred name resolution
    This example shows four procedures and the various ways that deferred name resolution can be used. Each stored procedure is created, although the table or column referenced does not exist at compile time.IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'proc1' AND type = 'P')
       DROP PROCEDURE proc1
    GO
    -- Creating a procedure on a nonexistent table.
    USE pubs
    GO
    CREATE PROCEDURE proc1
    AS
       SELECT *
       FROM does_not_exist
    GO  
    -- Here is the statement to actually see the text of the procedure.
    SELECT o.id, c.text
    FROM sysobjects o INNER JOIN syscomments c 
       ON o.id = c.id
    WHERE o.type = 'P' AND o.name = 'proc1'
    GO
    USE master
    GO
    IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'proc2' AND type = 'P')
       DROP PROCEDURE proc2
    GO
    -- Creating a procedure that attempts to retrieve information from a
    -- nonexistent column in an existing table.
    USE pubs
    GO
    CREATE PROCEDURE proc2
    AS
       DECLARE @middle_init char(1)
       SET @middle_init = NULL
       SELECT au_id, middle_initial = @middle_init
       FROM authors
    GO  
    -- Here is the statement to actually see the text of the procedure.
    SELECT o.id, c.text
    FROM sysobjects o INNER JOIN syscomments c 
       ON o.id = c.id
    WHERE o.type = 'P' and o.name = 'proc2'
      

  2.   

    存储过程最好的教程就是MSSQL的帮助。MSSQL的帮助不但是存储过程的最好教程,也是学习MSSQL的最好教程。好好看吧。基础的在那里就足够了。
      

  3.   

    看联机帮助,很详细的.有什么不明白的就问直接一点的说,存储过程就是一些SQL处理的集合
      

  4.   

    看联机帮助-----------------
    CREATE PROCEDURE
      

  5.   

    看联机帮助-----------------
    CREATE PROCEDURE
      

  6.   

    这样说吧,存储过程其实就是一个function(但有些不同)。每个数据库都会有function的啦。不同之处在于写function的语言不同,导致function和procedure的能力有些差别。著名的如pl/sql,大名在外啦,差一点的也有pl/pgsql,功能也很强。sqlserver写function和procedure的语言叫tsql(transact-sql),明白些了没?
      

  7.   

    这样说吧,存储过程其实就是一个function(但有些不同)。每个数据库都会有function的啦。不同之处在于写function的语言不同,导致function和procedure的能力有些差别。著名的如pl/sql,大名在外啦,差一点的也有pl/pgsql,功能也很强。sqlserver写function和procedure的语言叫tsql(transact-sql),明白些了没?