Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --My base case example
- DECLARE @zone_peril VARCHAR(MAX)
- DECLARE @input_table VARCHAR(MAX)
- DECLARE @SQL nvarchar(MAX)
- SET @input_table = '2018_1_PROJECTED_INPUT_AUAP'
- SET @SQL = 'set @zone_peril = right(''' + @input_table + ''', 4)'
- SELECT @SQL
- -- Returns set @zone_peril = select right('2018_1_PROJECTED_INPUT_AUAP', 4) which is correct
- SET @zone_peril = RIGHT(@input_table, 4)
- SELECT @zone_peril
- --Returns AUAP which is correct
- EXEC(@SQL)
- --Must declare the scalar variable "@zone_peril"
- --@zone_peril is being reset after using set @sql? here is where I'm stuck
- /* Ultimately all i'm trying to do is generate an output table name based on the name of an input table as follows: */
- ALTER PROCEDURE XYZ
- @cedant VARCHAR(MAX),
- @input_table VARCHAR(MAX)
- AS
- BEGIN
- DECLARE @SQL VARCHAR(MAX)
- DECLARE @output_table VARCHAR(MAX)
- DECLARE @zone_peril VARCHAR(MAX)
- SET NOCOUNT ON
- SET @SQL = 'set @zone_peril = right(''' + @input_table + ''', 4)
- set @output_table = ''2018_1_OUTPUT_'''+@cedant+'''_'''+@zone_peril+''''
- EXEC(@SQL)
- SELECT @SQL = 'Create table'+@output_table+'(column definitions etc...)'
- /*SCRIPT WHICH RUNS DATA INTO @output_table OK */
- END
- /*When I run the SP I don't get any error message but I don't get any output table either. I think its to do with the table names.
- In your post you mention dynamically assigning a value to @zone_peril is not a good idea, so I'm curious if you have other suggestions.
- Ultimately my goal is to be able to pass a list of input table names into the script and have it generate output tables */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement