Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:57:17+00:00 2026-06-14T18:57:17+00:00

I’m kinda new to using stored procedures, so I have stumbled upon two questions.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:57:18+00:00Added an answer on June 14, 2026 at 6:57 pm

    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 meant

    ranking_date <= @todaysDateminusthirty
    

    or

    ranking_date <= @todaysdate
    

    Or (still better, probably)

    ranking_date BETWEEN @todaysDateminusthirty AND @todaysdate
    

    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:

    SELECT
      ranking_position,
      ranking_date
    FROM dbo.t_ranking
    WHERE ranking_keyword = @keyword
      AND ranking_date BETWEEN DATEADD(day, -30, GETDATE()) AND GETDATE()
      AND ranking_id_doman IN (
        SELECT doman_id FROM dbo.t_doman WHERE doman_namn = @domannamn
      )
    ;
    

    You can see that I replaced @todaysDateminusthirty and @todaysdate with 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 EXISTS checks are not needed either: if there’s no match at any point (no @domannamn in t_doman or no @keyword in t_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:

    ALTER PROCEDURE [dbo].[fetchRankingData]
    @domannamn [varchar](100),
    @keyword [varchar](100)
    /* the OUTPUT parameters are removed as no longer needed apparently */
    AS
    /* no apparent need for additional variables either */
    BEGIN
    
    SET NOCOUNT ON;
    
    SELECT
      ranking_position,
      ranking_date
    FROM dbo.t_ranking
    WHERE ranking_keyword = @keyword
      AND ranking_date BETWEEN DATEADD(day, -30, GETDATE()) AND GETDATE()
      AND ranking_id_doman IN (
        SELECT doman_id FROM dbo.t_doman WHERE doman_namn = @domannamn
      )
    ;
    
    END
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.