syntax error

  • Hello,

    I am trying to create some sql job which runs every few minutes.It will use below code inside it.

    DECLARE @MaxMinutes int

    SET @MaxMinutes = 10

    DECLARE @Temp TABLE( spid int, job_name varchar(1000), program_name varchar(100), Minutes_Running varchar(10), last_batch datetime)

    INSERT INTO @Temp (spid, job_name, program_name, Minutes_running,last_batch)

    SELECT p.spid,j.name,p.program_name, isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) [MinutesRunning],

    last_batch FROM master..sysprocesses p

    JOIN msdb..sysjobs j ON dba_db.dbo.f_dba_SysJobs_GetProcessid(j.job_id) = substring(p.program_name,32,8)

    WHERE program_name like 'SQLAgent - TSQL JobStep (Job %'

    AND isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) > @MaxMinutes

    SELECT * FROM @TEMP

    IF @@ROWCOUNT <> 0

    BEGIN

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'Profile',

    @recipients = 'email@email.com',

    @query = @TEMP,

    @subject = 'test'

    --@attach_query_result_as_file = 1

    END

    ELSE

    PRINT 'No jobs running'

    GO

    But this is giving me error that u must declare scalar variable @TEMP.

    When I try this, @query = 'SELECT * FROM @TEMP'

    Then also I am getting this error,

    Msg 22050, Level 16, State 1, Line 0

    Error formatting query, probably invalid parameters

    Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 495

    Query execution failed: Msg 1087, Level 15, State 2, Server USNYPDBA01, Line 1

    Must declare the table variable "@TEMP".

    Any easiest workaround for this?

  • easy fix.

    @temp variables go out of scope when you call it in the msdb.dbo.sp_send_dbmail call , so you'll have to change it to a #temp table instead.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks a lot.

  • I don't think #temp will work, it would need to be a global ##temp table. The query is run from a separate connection, so it would not have access to regular #temp tables.

    A better approach would be to put your query in a view, then use the view as the sp_send_dbmail query parameter.

  • Thanks but we can not declare a variable inside view so have to use ## only. Thanks.

  • dallas13 (4/2/2015)


    Thanks but we can not declare a variable inside view so have to use ## only. Thanks.

    Be careful if you use this approach. Once you start using global temp tables concurrency can become a very serious issue.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Are you saying you can't use a view because of the @MaxMinutes variable? You couldn't just hardcode that in the view definition?

    How about making it a table-valued function instead of a view, then use "@query = 'SELECT * FROM dba_db.dbo.tvfRunningJobs(10)',"

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply