SELECT Statement in T-SQL

admin

Administrator
Staff member
I have created a stored procedure and want to use the following queries. It works but the scope of @Temp variable is lost when use in other query. Actually I want to use the value of @Temp variable in next INSERT Statement within the same stored procedure.
LIKE

DECLARE @TableName1 AS CHAR(25)
DECLARE @TableName2 AS CHAR(25)

SELECT @TableName1='TABLE1'
SELECT @TableName2='TABLE2'

EXEC ('DECLARE @Temp AS INTEGER SELECT @Temp=MAX(Col1) FROM ' +@TableName1)

EXEC ('INSERT INTO ' +@TableName2+
'VALUES ('+@Temp)

(Say TABLE2 has only one column)

Please comment it.
Akbar




(<!-- m --><a class="postlink" href="http://mondrian.sba.com/forums/Index.cfm">http://mondrian.sba.com/forums/Index.cfm</a><!-- m -->
 
Back
Top