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 6012467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:24:31+00:00 2026-05-23T02:24:31+00:00

declare @id int execute getLastRaw ‘subjectID’,’tblSubject’, @id output print @id alter procedure getLastRaw @column

  • 0
declare @id int
execute getLastRaw 'subjectID','tblSubject', @id output
print @id

alter procedure getLastRaw @column char(20), @tbl Char(20),
@return int output
as
declare @dynSQL varchar(100)
select @dynSQL ='SELECT TOP 1 '+@return+'='+ @column + ' FROM ' + @tbl + ' ORDER BY ' + @column + ' DESC'
exec(@dynsQL)

I want to get the value which gets selected from the select statement. But it says:

Msg 245, Level 16, State 1, Procedure getLastRaw, Line 5
Conversion failed when converting the varchar value 'SELECT TOP 1 ' to data type int.
  • 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-23T02:24:32+00:00Added an answer on May 23, 2026 at 2:24 am
    1. When you concatenate @return SQL Server tries to convert the whole expression to int because of datatype precedence

    2. You can’t assign @return outside or inside of the dynamic SQL because of scope: the stored proc and the dynamic SQL are different. Local variables are local to the scope only

    You’ll have to use a temp table: this is per connection and available for inner scopes (dynamic SQL)

    ...
    declare @dynSQL varchar(100)
    
    select @dynSQL ='SELECT TOP 1 '+ @column + ' FROM ' + @tbl + ' ORDER BY ' + @column + ' DESC'
    
    CREATE TABLE #result (rtn int)
    
    INSERT #result (rtn)
    exec(@dynsQL)
    
    SELECT @return = rtn FROM #result
    GO
    

    Although, it is just as easy and more correct to use a proper SELECT…

    declare @id int
    SELECT @id = subjectID FROM tblSubject ORDER BY subjectID DESC
    print @id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ALTER PROCEDURE dbo.ModeratorSpamDeleteComment DECLARE @CommentID int; AS BEGIN DELETE Comments WHERE CommentsID=@CommentID END It
I have a simple C function which I declare as: int strLen(const char* str_)
Im not able to execute below query.Please help. Declare @i Int Set @i=1 Exec
i have this stored procedure in the update command of formview DECLARE @ResultTelID int
I have A stored procedure written DECLARE @AreaID AS INT DECLARE @DayPrior AS INT
I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int
Given the following: declare @a table ( pkid int, value int ) declare @b
Here's my query: DECLARE @StartRow INT DECLARE @PageSize INT SET @StartRow = 1 SET
Let's say I declare this: int [a][b][c]; Does a stand for the level/page, b
I have a sql query as follows: Declare @DivisionNo INT SET @DivisionNo = 5117

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.