Hello,
I am using some code to dynamically create a table. The columns of the table are determined at run time.
The code is creating the proper TSQL statement and when I execute the statement independently it creates the table. However, in the code body after Executing the TSQL when I try to reference the table, it gives an "Invalid Object Name #Input_Word_Chars " error, where #Input_Word_Chars is the table that is created in the Dynamic T-SQL statment
Code snippet is as follows:
************************************************************
set @Len =0
While(@Len < @Unique_W_Len)
BEGIN
set @curr_char = substring(@unique_word_string , @Len+1, 1)
if(@curr_char != ' ')
begin
set @input_table_string = @input_table_string + ' '
+ @curr_char + ' float, '
END
Set @Len = @Len + 1
END
set @input_table_string = @input_table_string + ' word varchar(80) )'
Print @input_table_string
Exec (@input_table_string)
Insert Into #Input_word_Chars (word) values (@input_word)
************************************************************
Error occurs at the last line " Insert Into
#Input_word_chars ... "
Exec(@input_table_string) creates the table
The print output for @input_table_string is:
*********************************************************
Create Table #Input_word_Chars ( t float, e float, s float, word varchar(80) )
I am using some code to dynamically create a table. The columns of the table are determined at run time.
The code is creating the proper TSQL statement and when I execute the statement independently it creates the table. However, in the code body after Executing the TSQL when I try to reference the table, it gives an "Invalid Object Name #Input_Word_Chars " error, where #Input_Word_Chars is the table that is created in the Dynamic T-SQL statment
Code snippet is as follows:
************************************************************
set @Len =0
While(@Len < @Unique_W_Len)
BEGIN
set @curr_char = substring(@unique_word_string , @Len+1, 1)
if(@curr_char != ' ')
begin
set @input_table_string = @input_table_string + ' '
+ @curr_char + ' float, '
END
Set @Len = @Len + 1
END
set @input_table_string = @input_table_string + ' word varchar(80) )'
Print @input_table_string
Exec (@input_table_string)
Insert Into #Input_word_Chars (word) values (@input_word)
************************************************************
Error occurs at the last line " Insert Into
#Input_word_chars ... "
Exec(@input_table_string) creates the table
The print output for @input_table_string is:
*********************************************************
Create Table #Input_word_Chars ( t float, e float, s float, word varchar(80) )