Johnathan Johnathan - 6 months ago 16
SQL Question

SQL script troubles

I am trying to retrieve employeeIDs using the following sql script:

SELECT * from tblEmployees
GO
CREATE PROCEDURE spGetEmployeeID10
AS
BEGIN
SELECT employeeID
FROM tblEmployees
END


The script runs but returns the entire table with all of the columns' names: employeeID, lastName, firstName, Comment. However, it should only return the employeeID column, no? Am I making an obvious mistake? I am using SQL server management studio 2014. Also, whenever I modify a script and try to re-run it again, I get an error saying that the script already exits. Is there a way to simply edit the script and re-run it with the same name?
Thank you!

Answer

"Am I making an obvious mistake?" yes, I'm afraid so, the first select statement is returning everything, an the second is creating a stored procedure. My best guess is that you want this:

CREATE PROCEDURE spGetEmployeeID10
AS
BEGIN 
    SELECT employeeID
    FROM tblEmployees
    --my guesswork come further and I added next line
    where employeeID = 10
END
GO

EXEC spGetEmployeeID10
GO