I’m kinda new to using stored procedures, so I have stumbled upon two questions.
First why am I not allowed to do this? Error message says:
Operand type clash: date is incompatible with int
Code :
@time date OUTPUT
SELECT @time = ranking_date
FROM [dbo].[t_ranking]
WHERE ranking_date <= DATEDIFF(day, @todaysDateminusthirty, @todaysdate)
AND ranking_keyword = @keyword
AND ranking_id_doman = @domainID
Onwards how can I return my result as a complete dataset? Instead of saving into two variables? Or can they hold more than one row?
ALTER PROCEDURE [dbo].[fetchRankingData]
-- Add the parameters for the stored procedure here
@domannamn [varchar](100),
@keyword [varchar](100),
@rankingen [decimal](6,2) OUTPUT,
@time date OUTPUT
AS
DECLARE @domainID int
DECLARE @todaysDateminusthirty datetime
DECLARE @todaysdate datetime
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT 1 FROM [dbo].[t_doman] WHERE doman_namn = @domannamn)
BEGIN
set @todaysdate = getdate()
set @todaysDateminusthirty = DATEADD(day,-30,@todaysdate)
SELECT @domainID = doman_id FROM [dbo].[t_doman] WHERE doman_namn = @domannamn
IF EXISTS(SELECT 1 FROM [dbo].[t_ranking] WHERE ranking_id_doman = @domainID AND ranking_keyword = @keyword)
BEGIN
SELECT @rankingen = ranking_position FROM
[dbo].[t_ranking] WHERE ranking_keyword = @keyword
AND ranking_id_doman = @domainID
SELECT @time = ranking_date FROM
[dbo].[t_ranking] WHERE ranking_date <= DATEDIFF(day,@todaysDateminusthirty,@todaysdate) AND ranking_keyword = @keyword
AND ranking_id_doman = @domainID
END
END
END
First of all, why are you comparing a date to a number of days? For that is what
DATEDIFF(DAY, ...)essentially returns, the number of days between two dates. Perhaps you meantor
Or (still better, probably)
As for your second problem, no, you can’t store rows into scalar variables, but you can have your stored procedure return a result set: just use a SELECT statement that returns rows (as opposed to one that initialises variables), i.e., in other words, a “normal” SELECT statement.
The purpose of your stored procedure isn’t very clear to me, so my suggestion below may not correlate well with it. But that might be just as well, as you need to do your homework too, don’t you. It’s just that you may need a good starting point, and the following should (hopefully) provide you with one:
You can see that I replaced
@todaysDateminusthirtyand@todaysdatewith the corresponding expressions directly in the query. If you think you need those variables (e.g. you may be thinking of extending this procedure, and so the variables could be re-used in other parts of the body), leave them in place then. As it is, however, your stored procedure does not seem to require them.Note also that your
IF EXISTSchecks are not needed either: if there’s no match at any point (no@domannamnint_domanor no@keywordint_ranking), the result will be just an empty dataset. And that should be fine, and the module calling this SP should just check for the existence of rows in the resulting set to account for that case.Ultimately, then, the entire declaration could look like this:
I hope I will not offend you (too much) when I take this opportunity and suggest you start reading more on the topic of stored procedures in the manuals and elsewhere. Books Online should give you enough good material to start your improvement, and browsing/searching StackOverflow wouldn’t be a bad idea either.