Solved

Exec(SQLString) does not return an Output value in SQL Server 2005

Posted on 2010-08-27
2
444 Views
Last Modified: 2012-05-10
If have the following stored procedure called from a VB.Net program. The stored procedure should return @ColumnValue as Output to calling program, but it doesn't. If I execute the stored procedure in SQL Server Management Studio the value is NULL. If I change @SQLString to  .... SELECT ' + quotename(@ColumnName) + ' FROM ' .... I get the correct value. Btw T_NAM is a user-defined data type.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

Alter PROCEDURE [dbo].[WP_SP_GET_TEXT_CUSTOM_FIELD]
       @TableName T_NAM,
       @ColumnName T_NAM,
       @WhereColumn T_NAM,
         @Criteria T_NAM,
       @ColumnValue T_NAM Output

AS

BEGIN
      
      Declare @SqlString varchar(1000)
            
      Begin Try
      
            Set @SqlString = 'SET QUOTED_IDENTIFIER OFF
                                   SELECT ' + @ColumnValue + ' = '  + quotename(@ColumnName)
                                             + ' FROM ' + quotename(@TableName)
                                   + ' WHERE ' + quotename(@WhereColumn) + ' = "' + @Criteria + '"'
      
      Exec(@SqlString)
      
      Return 0

      End Try

      Begin Catch
            
            Return ERROR_NUMBER()

      End Catch

END

The program calls the stored procedure as follows:

dbEmployees.WP_SP_GET_TEXT_CUSTOM_FIELD("PAYROLL_EMPL_CUSTOM_INFO", _
sqlCustomControls.FIELD_NAME, "EMPLOYEE_NO", txtEmplNo.Text, strTextValue)
devCtrl.Text = strTextValue


0
Comment
Question by:bobbysmarina
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 33546919

Alter PROCEDURE [dbo].[WP_SP_GET_TEXT_CUSTOM_FIELD]

       @TableName T_NAM,

       @ColumnName T_NAM,

       @WhereColumn T_NAM,

         @Criteria T_NAM,

       @ColumnValue T_NAM Output



AS



BEGIN

      

      Declare @SqlString varchar(1000)

      declare @tmp table (T T_NAM)

            

      Begin Try

      

            Set @SqlString = 'SET QUOTED_IDENTIFIER OFF

                                   SELECT ' + quotename(@ColumnName)

                                             + ' FROM ' + quotename(@TableName)

                                   + ' WHERE ' + quotename(@WhereColumn) + ' = "' + @Criteria + '"'

      INSERT @tmp

      Exec(@SqlString)

      

      SELECT @ColumnValue = T from @tmp

      Return 0



      End Try



      Begin Catch

            

            Return ERROR_NUMBER()



      End Catch



END

Open in new window

0
 

Author Comment

by:bobbysmarina
ID: 33547439
That works. Thank you very much.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now