|
Posted by Paul on 04/20/07 11:13
Hi,
I would do it using a procedure as follows:
CREATE PROCEDURE GetFieldValue
@strTableName VARCHAR(250),
@strFieldName VARCHAR(250),
@nID DECIMAL(9,0),
@strResult VARCHAR(250) OUTPUT
AS
BEGIN
DECLARE @strSQL NVARCHAR(4000)
SET @strSQL = 'SELECT @strResult = ' + @strFieldName
+ ' FROM ' + @strTableName
+ ' WHERE ID = ' + CAST (@nID AS VARCHAR(9))
EXEC sp_executesql @strSQL,
N'@strResult VARCHAR(250) OUTPUT',
@strResult OUTPUT
END
GO
And then execute it in a way like this:
DECLARE
@strValue VARCHAR(250)
BEGIN
EXEC dbo.GetFieldValue 'TABLE1', 'COLUMN1', 1, @strValue OUTPUT
PRINT @strValue
END
GO
Obviously the print is just to ensure that you have the correct value.
This may not be the best way, but it's a way that works for me.
Hope this helps,
Paul
[Back to original message]
|