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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:50:00+00:00 2026-06-10T06:50:00+00:00

I am trying to create a stored that will accept two values, a column

  • 0

I am trying to create a stored that will accept two values, a column name and a value. It will then check if there is a record that exists for the passed in column name with the passed in value. I’ve tried the following

CREATE PROCEDURE p_HasActiveReference
     @pi_colName varchar(100)
    ,@pi_colValue varchar(100)
AS
BEGIN
SET NOCOUNT ON      
    declare @sql varchar(1000)

    set @sql = 'IF EXISTS(SELECT TOP 1 p.PaymentId
                FROM Payments p
                WHERE ' + @pi_colName + ' = ' + @pi_colValue +     'AND Active = 1)

                    SELECT ''True'' AS RETVAL
                ELSE
                    SELECT ''False'' AS RETVAL'
    exec(@sql)  
 END

However, I always get this error

Conversion failed when converting the varchar value ‘InternalOr’ to data type int.

When I call the procedure with the following

p_HasActiveReference 'InternalOrgCode', '10110'

The internalOrgCode column is of value varchar(10)

I am not a SQL expert, so I am not even sure if what I need to achieve is even possible using that technique!

Thanks!

  • 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-10T06:50:01+00:00Added an answer on June 10, 2026 at 6:50 am

    At least one issue: you should be surrounding your string value with single quotes, and to escape those inside a string you need to double them up:

    WHERE ' + @pi_colName + ' = ''' + @pi_colValue + ''' AND ...
    

    You also may want to declare your @sql variable as something bigger than 100 characters! Looks like your string is getting truncated.

    If the possible values for @pi_colName are finite, the data type is always string, and the columns are collation compatible, you could do something like this and avoid dynamic SQL:

    SELECT ...
    WHERE CASE @pi_colName 
      WHEN 'col1' THEN col1
      WHEN 'col2' THEN col2
    END = @pi_ColValue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a stored procedure that will be used by a client
Im trying to create a script that will post a notification stored in a
I'm trying to create a stored proc that indicates a pay raise for two
I am trying to create a stored procedure that will take a row with
I am trying to create a BEFORE INSERT trigger that will check the incoming
I am trying to create a trigger that will update a GEOMETRY column based
I am trying to create a Stored Procedure that will be used for a
I'm trying to write a stored procedure that will create a new FILEGROUP based
I am trying to write a stored procedure that will check for the existence
I'm trying to create a 2D array to store some values that don't change

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.