-5

I have this query that I want to use as a stored procedure:

  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)

it should return values of Expr1 and Expr2

I've tried to declare inputs and outputs but with no luck

please help

EDIT :

  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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M.A.
  • 1
  • 3
  • 9
  • 2
    Can you post what you tried? What error did you get? – Mark Byers Jun 16 '12 at 20:14
  • 2
    ...and what "with no luck" means... – Aaron Bertrand Jun 16 '12 at 20:15
  • Check the data type in the variable declaration of @VID. – JaggenSWE Jun 16 '12 at 20:17
  • The main purpose so i can use this query in linq , so i thought i can make it as stored procude to use IsNull or Select COALESCE as stored procedure – M.A. Jun 16 '12 at 20:19
  • You Are Right @AaromBertrand , Most of my questions are unanswered , yet the answered question where really helpful , i'll be paying attention to them , thank you – M.A. Jun 16 '12 at 20:22
  • 4
    Why did you [completely abandon this related question](http://stackoverflow.com/questions/11064177/using-isnull-or-select-coalesce-in-linq)? There seemed to be an answer there that you could use, but all you did was reply and say you got "an error." The person even followed up and asked "what error?" Why wouldn't you answer and explain the problem? – Aaron Bertrand Jun 16 '12 at 20:26
  • He is saying your answered questions are NOT ACCEPTED BY YOU, not unanswered. – user1166147 Jun 17 '12 at 14:44

3 Answers3

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
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • It returns an error message : – M.A. Jun 16 '12 at 20:32
  • 3
    TELL. US. THE. ACTUAL. ERROR. MESSAGE. Why is that so hard? – Aaron Bertrand Jun 16 '12 at 20:32
  • I did , the Comment system didn't allow me since i was late on editing : – M.A. Jun 16 '12 at 20:36
  • It returns an error message : Incorrect Syntax Near "@lName ,Must Declare the scaler variable "@lName" , Incorrect syntax – M.A. Jun 16 '12 at 20:37
  • 1
    See how much more helpful that is? That allowed me to spot that I missed a syntax error in your original code. – Aaron Bertrand Jun 16 '12 at 20:38
  • Thank You Aaron for your effort , it returns error message "Incorrect Syntax near "GO" " – M.A. Jun 16 '12 at 21:00
  • 1
    Where are you running this? What else are you running with it? Details, details, details. If you are running this from your app, stop doing that. Create (or alter, if it already exists) the procedure in a query window in Management Studio. Or, if you're getting a syntax error near `GO`, you could try to run the code without the `GO` from wherever you're running it. – Aaron Bertrand Jun 16 '12 at 21:02
2

The short answer is the below stored procedure, modified from @Aaron Bertrand's CORRECT answer, will return the results you want in the way it seems like you need the data to me.

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

There are several things going on here. I am going to walk you through exactly what I did in and effort to help you and explain how to move forward, as this is this 4th question you have opened related to this in the last couple of days.

OK - @Aaron Bertrand's stored procedure is good. He took the query you provided in your question and made it work and gave you a great answer to your question. However, I think there are some problems with your question. To get good answers you need to ask a good question.

I went to ONE of the other related questions you already asked and got your table structures and IN MANAGEMENT STUDIO: created the tables and inserted your provided data , compiled his sproc locally and got:

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'.

I know from the table structures from your OTHER question that I needed to change the joins:

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

I copied the variable declaration and the query outside of the stored procedure create statement and made the changes I mentioned and set the variables and highlighted and ran JUST this. It worked. You can do this to test if there is simply a syntax or simple problem with your query when you are having problems with a stored proc.

I made the just tested changes to the JOINS within the stored procedure. I compiled it and successfully ran it.

Here's how to run a stored proc from Management Studio (NOTE I use EXEC in MS testing because its easy to type, but LEARN the difference between SP_EXECUTESQL and EXEC):

Execute a stored procedure:

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

You said "I've tried to declare inputs and outputs but with no luck". I don't think you really want Output Variables - and it matters. I have a very strong feeling you want it returned as a Result Set. Do some research and LEARN about the different ways to return data from stored procedures. So anyway - here you go - Drop or Alter your old sproc and try this - it is quickly modified from @Aaron's and has no output variables - it is the same as the first one in the beginning of my answer.

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 

Run it in SSMS using 'EXEC' (management studio):

EXEC [GetDepartmentName] 1

Returns:

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

EDITED - DECIDED TO REMOVE SOME STUFF

Community
  • 1
  • 1
user1166147
  • 1,570
  • 2
  • 15
  • 17
0

Assuming your select works when you run it, i.e. that the joins are correct and this would return data, then the create procedure script below should do what you need.

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)

If you just need to return the Expr1 and Expr2 values there is no need to define them as OUTPUT parameters, as in your edit. Anything that's returned by the select statement will be present in your results.

Try running the procedure from the web page but with SQL Profiler to see whether your @VID parameter is passed as you expect.

Simon Martin
  • 4,203
  • 7
  • 56
  • 93