Hi ahmadsubuhanl...,
You need to make use of sp_executesql function and pass the parameter value as in the example.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
SQL
DECLARE @sqlCommand NVARCHAR(1000),
@Country NVARCHAR(50) = 'India',
@ColumnName NVARCHAR(MAX) = 'Name'
DECLARE @ReturnValue NVARCHAR(MAX)
SET @sqlCommand = 'SELECT @Name = ' + @ColumnName + ' FROM Customers WHERE Country = ''' + @Country + ''''
EXECUTE sp_executesql @sqlCommand, N'@Name NVARCHAR(MAX) OUTPUT', @Name = @ReturnValue OUTPUT
SELECT @ReturnValue
Output
Mudassar Khan
Here is your query.
ALTER PROCEDURE [BacaRumus]
@no_induk NVARCHAR(50),
@rumus1 NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ts NVARCHAR(1000), @ReturnValue NVARCHAR(MAX)
SET @ts = 'SELECT @Return = ' + @rumus1 + ' FROM tblTotAbsen WHERE no_induk = ''' + @no_induk + ''''
EXECUTE sp_executesql @ts, N'@Return NVARCHAR(MAX) OUTPUT', @Return = @ReturnValue OUTPUT
SELECT @ReturnValue
END