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

  • SEARCH
  • Home
  • 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 6656035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:35:38+00:00 2026-05-26T01:35:38+00:00

Please forgive newbie’s ignorance! How do I grab the value of the output parameter

  • 0

Please forgive newbie’s ignorance!

How do I grab the value of the output parameter in execute sp_executesql?

I can see the output but cannot get to it:

    DECLARE @LastActivity nvarchar(100)
    DECLARE @LastActivityDate datetime
    DECLARE @sql nvarchar(MAX)
    DECLARE @RowsToProcess  int 
    DECLARE @CurrentRow     int 
    DECLARE @SelectCol1     nvarchar(100)
    DECLARE @SelectCol2     nvarchar(100)
    DECLARE @SelectCol3     nvarchar(100)
    DECLARE @LastDate TABLE (RowID int not null primary key identity(1,1), col4 nvarchar(MAX), col5 sql_variant)
    DECLARE @table1 TABLE (RowID int not null primary key identity(1,1), col1 nvarchar(100),col2 nvarchar(100),col3 nvarchar(100))
    INSERT into @table1 (col1,col2,col3)(SELECT t.name AS col1, c.name AS col2, m.Field1 as col3 
                                         FROM sys.columns c INNER JOIN 
                                              sys.tables t ON c.object_id = t.object_id INNER JOIN 
                                              sys.schemas s ON t.schema_id = s.schema_id INNER JOIN 
                                              dbo.MERGE_TABLES m ON m.Table_Name=t.name
                                              WHERE c.name LIKE '%[_]DATE%' and m.[Enabled]='Y')  
    SET @RowsToProcess=@@ROWCOUNT 
    SET @CurrentRow=0 
    WHILE @CurrentRow<@RowsToProcess 
        BEGIN 
        SET @CurrentRow=@CurrentRow+1 
        SELECT @SelectCol1=col1,@SelectCol2=col2,@SelectCol3=col3 FROM @table1 WHERE RowID=@CurrentRow
            SET @sql='SELECT ' + '[dbo].[ConvertToDatetime](MAX(' + @SelectCol2 + '))' + ' FROM ' + @SelectCol1 + ' Where ' + @SelectCol3 + ' = ' + '''0722607QZ'''
        Declare @params as nvarchar(MAX)
        Set @params = '@date sql_variant output'
        Declare @date as sql_variant;
        execute sp_executesql 
                 @sql    
                ,@params 
                ,@date output
                       Select @date
        INSERT into @LastDate VALUES (@sql, @date) 
        end   
select col4,col5 from @LastDate

select col4,col5 from @LastDate gives me the SQL script in clo4 but col5 is empty! I need to store the @date as I still need to get the Max(@date)

Thanx a million.

SET @sql=’set @date =(‘SELECT ‘ + ‘[dbo].[ConvertToDatetime](MAX(‘ +
@SelectCol2 + ‘))’ + ‘ FROM ‘ + @SelectCol1 + ‘ Where ‘ + @SelectCol3
+ ‘ = ‘ + ”’0722607QZ”’ ) ‘

the above sql gives me error: Incorrect syntax near ‘.’

SET @sql=’set @date =(SELECT [dbo].[ConvertToDatetime](MAX( + @SelectCol2 + ))
FROM @SelectCol1 Where @SelectCol3 ”=0722607QZ” ) ‘

The above sql gives the error: Must declare the scalar variable “@SelectCol2”

SET @sql='SELECT ' + @date + '=convert(nvarchar(100), [dbo].[ConvertToDatetime](MAX(' + @SelectCol2 + ')))' + ' FROM ' + @SelectCol1 + ' Where ' + @SelectCol3 + ' = ' + '''0722607QZ''' 

the above produces the error : Implicit conversion from data type sql_variant to nvarchar is not
allowed. Use the CONVERT function to run this query.

SET @sql='SELECT ' + @date + '=convert(nvarchar(MAX),(MAX(' + @SelectCol2 + '))' + ' FROM ' + @SelectCol1 + ' Where ' + @SelectCol3 + ' = ' + '''0722607QZ''' 

the above produces no error but all output is NULL, no values.

  • 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-05-26T01:35:38+00:00Added an answer on May 26, 2026 at 1:35 am

    Your syntax looks ok but you never assign to the output variable @date hence no value.

    Instead of;

    SET @sql='SELECT ...'
    

    You need;

    SET @sql='set @date = (SELECT ...'
    

    Can’t you use a better type than sql_variant?

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

Sidebar

Related Questions

Please forgive my ignorance, but I wasn't able to find an answer elsewhere. I
Please forgive my ignorance and poor SQL programming skills but I am normally a
please forgive my windows ignorance, Is there an application which can show me which
I am a dojo newbie so please forgive my ignorance. I have a dojox.mobile.EdgeToEdgeDataList
Please forgive the ignorance, but I'm very new to PHP and I couldn't find
Please forgive my ignorance, but I am very new to IOC and NinJect. I
Please forgive my ignorance, but I am noob. I have 4 Macs in my
XML/XSL newbie here, so please forgive my ignorance. This is the xml input given
I've just started using Jasmine so please forgive the newbie question but is it
I'm a brand-newbie to C#, albeit not programming, so please forgive me if I

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.