Solved

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

Posted on 2010-08-27
2
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

738 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