Guest_imported
New member
- Jan 1, 1970
- 0
I need to be able to dynamically run the following statement as part of a Stored Procedure. The value of the count needs to be passed to the @test variable:
declare @test char(10)
select @test = count(*) from @tablename where @fieldname in ('FALSE','TRUE')
exec @test
In order to do this I ran this test statement:
declare @tablename varchar(100)
set @table = 'test_table
declare @fieldname varchar(100)
set @fieldname = 'test_field'
declare @test char(10)
declare @script varchar(255)
set @script =
('select @test = count(*) from ' + @tablename + ' where rtrim(' + @fieldname + ') in (''FALSE'',''TRUE'')')
print @script
print char(013)
execute @script
when printing the @script variable, I get the following:
select @test = count(*) from test_table where rtrim(test_field) in ('FALSE','TRUE')
which is what I want, however when the @script string is executed, I get the following error message:
Could not find stored procedure 'select @test = count(*) from test_table where rtrim(test_field) in ('FALSE','TRUE')'.
Can anyone help, as it is driving me crazy ?
declare @test char(10)
select @test = count(*) from @tablename where @fieldname in ('FALSE','TRUE')
exec @test
In order to do this I ran this test statement:
declare @tablename varchar(100)
set @table = 'test_table
declare @fieldname varchar(100)
set @fieldname = 'test_field'
declare @test char(10)
declare @script varchar(255)
set @script =
('select @test = count(*) from ' + @tablename + ' where rtrim(' + @fieldname + ') in (''FALSE'',''TRUE'')')
print @script
print char(013)
execute @script
when printing the @script variable, I get the following:
select @test = count(*) from test_table where rtrim(test_field) in ('FALSE','TRUE')
which is what I want, however when the @script string is executed, I get the following error message:
Could not find stored procedure 'select @test = count(*) from test_table where rtrim(test_field) in ('FALSE','TRUE')'.
Can anyone help, as it is driving me crazy ?