I have a simple table:
CREATE TABLE [dbo].[test]
(
[eins] [varchar](50) NOT NULL,
[zwei] [varchar](50) NULL,
CONSTRAINT [PK_test]
PRIMARY KEY CLUSTERED ([eins] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
with two columns eins
und zwei
, both varchar(50)
with the values
insert into test(eins, zwei) values(1,2)
1 and 2 in the corresponding columns.
The query
select eins from test
gives the correct result of 1
the following code also gives the correct result of 1
in the results window:
declare
@in varchar(50),
@sql nvarchar(500)
set @in = 'eins'
set @sql = 'select ' + @in + ' from test'
Exec(@sql)
However, it doesn't make use of an output parameter and I need the result for further processing.
So, I try:
exec sp_executesql N' Select @1 from test where zwei = @2',N'@1 nvarchar(100),@2 nvarchar(100)',@1=N'eins',@2=N'2'
with an expected result of 1
. However: the result is eins
, i.e., the column name, not the value.
How can I query for something like Select @Variable from @Variable2 where @variabel3 = @Variable4
?
The table and columns can be non-variable, if need be, what's primarily important is, the Select @Variable
. I need this value for further processing.