I got simple 2 SQL Server stored procedure questions that need your help. I'm a newbie to SQL Server, so I got stuck with very simple syntax errors when I execute my code
- I got a table as below and I need to create a stored procedure named
count_status
that accepts a single argument and returns only the total number of animals of a particular conversation status and the code should not return the corresponding conservation status.
Species table
My code:
CREATE PROCEDURE count_status
@ConservationStatus INT
AS
SELECT COUNT(ID)
FROM Species
WHERE ConservationStatus = @ConservationStatus
GO;
END
And I got a syntax error for the above code. Could you help me with this?
Thanks
- Create a stored procedure named
format_currency
that accepts a character and a double number. It will return avarchar(32)
with the symbol in the front, followed by the number to 2 decimal places. For exampleformat_currency ('$',123.4)
should return$123.4
I don't know how to write code for this.
Thanks.
3条答案
按热度按时间acruukt91#
Explanation : 1) Use Begin After As 'this begins the procedure query' 2) Use Go After end
yvgpqqbh2#
For #1, I haven't tested this but I think you need to use a group by clause
For #2, note the input parm is decimal (12, 2), that defines 2 decimal places which should both pad anything like 123.1 and trim anything like 123.123
if you have a current version of Sql Server you can also use this
6jjcrrmo3#
In this way we can solve the second task using stored procedure in SQL Server:
Hope it helps.