选择语句作为存储过程

时间:2012-06-16 20:13:19

标签: sql-server-2008 tsql stored-procedures

我有这个查询,我想用作存储过程:

  SELECT ISNULL(P.firstname, s.firstname) AS Expr1,ISNULL(P.lastname,
  s.lastname) AS Expr2 FROM comment AS C LEFT OUTER JOIN professor AS P ON P.ID =   

  C.PID LEFT OUTER JOIN student AS s ON s.ID = C.SID

  WHERE (C.VID = @VID)

它应该返回Expr1和Expr2的值

我试图声明输入和输出,但没有运气

请帮助

编辑:

  CREATE PROCEDURE [dbo].[GetDepartmentName]
  @ID int,
  @fName varchar(50) OUTPUT
   @lName varchar(50) OUTPUT
  AS


SELECT @fName=COALESCE(p.firstname, s.firstname)   @lName=COALESCE(p.lastname,     

   s.lastname)as 
  FROM comment c
  LEFT JOIN Professor p
    ON c.pid = p.id
  LEFT JOIN Student s
    ON c.sid = s.id
 WHERE c.vid = @ID

3 个答案:

答案 0 :(得分:5)

ALTER PROCEDURE [dbo].[GetDepartmentName]
  @ID    INT,
  @fName varchar(50) OUTPUT, -- added missing comma here
  @lName varchar(50) OUTPUT
AS
BEGIN
  SET NOCOUNT ON;

  SELECT  
    @fName = COALESCE(p.firstname, s.firstname), -- added missing comma here
    @lName = COALESCE(p.lastname,  s.lastname)   -- removed strange "as" here 
  FROM dbo.comment AS c
  LEFT OUTER JOIN dbo.Professor AS p
    ON c.pid = p.id
  LEFT OUTER JOIN dbo.Student AS s
    ON c.sid = s.id
  WHERE c.vid = @ID;
END
GO

答案 1 :(得分:2)

简短的回答是下面的存储过程,从@Aaron Bertrand的CORRECT 回答修改,将以您需要数据的方式返回您想要的结果。

CREATE PROCEDURE [dbo].[GetDepartmentName]
  @ID    INT 
  AS
  BEGIN
  SET NOCOUNT ON;

  SELECT  
  COALESCE(p.firstname, s.firstname) as firstname,  
  COALESCE(p.lastname,  s.lastname) as lastname 
  FROM dbo.comment AS c
  LEFT OUTER JOIN dbo.Professor AS p
  ON c.pid = p.pid
  LEFT OUTER JOIN dbo.Student AS s
  ON c.sid = s.sid
  WHERE c.vid = @ID;
  END
  GO

这里有几件事情。我将引导您完成我所做的工作并努力帮助您并解释如何继续前进,因为这是您在过去几天内与此相关的第四个问题。

好的 - @Aaron Bertrand的存储过程很好。他接受了您在问题中提供的查询并使其正常工作,并为您提供了一个很好的答案。但是,我认为您的问题存在一些问题。 要获得好的答案,您需要提出一个好问题

我去了other related questions you already asked之一,得到了你的桌子结构和IN MANAGEMENT STUDIO:创建了表格并插入你提供的数据,在本地编译了他的sproc并得到了:

Msg 207, Level 16, State 1, Procedure GetDepartmentName, Line 14
Invalid column name 'id'.
Msg 207, Level 16, State 1, Procedure GetDepartmentName, Line 16
Invalid column name 'id'.

我从您的OTHER问题的表格结构中知道我需要更改连接:

LEFT OUTER JOIN dbo.Professor AS p
    ON c.pid = p.pid -- changed from c.pid = p.id, p.id is not a column
LEFT OUTER JOIN dbo.Student AS s
    ON c.sid = s.sid -- changed from c.pid = s.id, s.id is not a column

我在存储过程create语句之外复制了变量声明和查询,并进行了我提到的更改并设置了变量并突出显示并运行JUST this。有效。当您遇到存储过程问题时,可以执行此操作来测试查询是否存在语法或简单问题。

我对存储过程中的JOINS进行了刚刚测试的更改。 我编译了它并成功运行了它。

以下是如何从Management Studio运行存储过程(注意我在MS测试中使用EXEC,因为它易于键入,但了解SP_EXECUTESQL和EXEC之间的差异)

Execute a stored procedure:

[ [ EXEC [ UTE ] ] 
    { 
        [ @return_status = ] 
            { procedure_name [ ;number ] | @procedure_name_var 
    } 
    [ [ @parameter = ] { value | @variable [ OUTPUT ] | [ DEFAULT ] ] 
        [ ,...n ] 
[ WITH RECOMPILE ]

你说“我试图申报输入和输出,但没有运气”。我不认为你真的想要输出变量 - 这很重要。我有一种强烈的感觉,你希望它作为结果集返回。做一些研究并了解从存储过程返回数据的不同方法。所以无论如何 - 你去 - DropAlter你的旧sproc并尝试这个 - 它很快从@Aaron's修改而且没有输出变量 - 它与开头的第一个相同我的回答。

CREATE PROCEDURE [dbo].[GetDepartmentName]
  @ID    INT 
  AS
  BEGIN
  SET NOCOUNT ON;

 SELECT  
 COALESCE(p.firstname, s.firstname) as firstname,  
 COALESCE(p.lastname,  s.lastname) as lastname 
 FROM dbo.comment AS c
 LEFT OUTER JOIN dbo.Professor AS p
    ON c.pid = p.pid
 LEFT OUTER JOIN dbo.Student AS s
    ON c.sid = s.sid
 WHERE c.vid = @ID;
END
GO 

使用“EXEC”(管理工作室)在 SSMS 中运行:

EXEC [GetDepartmentName] 1

返回:

firstname   lastname
--------------------
mark    abram
john    adam
sean    hoak

已编辑 - 决定删除某些东西

答案 2 :(得分:0)

假设您的选择在运行时有效,即连接正确并且这将返回数据,那么下面的创建过程脚本应该可以满足您的需要。

CREATE PROCEDURE uspYourProcedureName 
  @VID INT
  AS 
  SET NOCOUNT ON;

  SELECT ISNULL(P.firstname, s.firstname) AS Expr1,
         ISNULL(P.lastname, s.lastname) AS Expr2 
  FROM   comment AS C 
         LEFT OUTER JOIN professor AS P ON P.ID = C.PID 
         LEFT OUTER JOIN student AS s ON s.ID = C.SID
  WHERE (C.VID = @VID)

如果您只需要返回Expr1和Expr2值,则无需像编辑那样将它们定义为OUTPUT参数。 select语句返回的任何内容都将出现在结果中。

尝试从网页运行该过程,但使用SQL事件探查器查看您的@VID参数是否按预期传递。