Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.60 KB | None | 0 0
  1. --My base case example
  2.  
  3. DECLARE @zone_peril VARCHAR(MAX)
  4. DECLARE @input_table VARCHAR(MAX)
  5. DECLARE @SQL nvarchar(MAX)
  6.  
  7.     SET @input_table = '2018_1_PROJECTED_INPUT_AUAP'
  8.  
  9.  
  10.     SET @SQL = 'set @zone_peril =  right(''' + @input_table + ''', 4)'
  11.     SELECT @SQL
  12.     -- Returns set @zone_peril = select right('2018_1_PROJECTED_INPUT_AUAP', 4) which is correct
  13.    
  14.     SET @zone_peril =  RIGHT(@input_table, 4)
  15.     SELECT @zone_peril
  16.     --Returns AUAP which is correct
  17.    
  18.     EXEC(@SQL)
  19.     --Must declare the scalar variable "@zone_peril"
  20.     --@zone_peril is being reset after using set @sql? here is where I'm stuck
  21.  
  22.  
  23. /* Ultimately all i'm trying to do is generate an output table name based on the name of an input table as follows: */
  24.  
  25. ALTER PROCEDURE XYZ
  26.  
  27. @cedant VARCHAR(MAX),
  28. @input_table VARCHAR(MAX)
  29.  
  30. AS
  31. BEGIN
  32.     DECLARE @SQL VARCHAR(MAX)
  33.     DECLARE @output_table VARCHAR(MAX)
  34.     DECLARE @zone_peril VARCHAR(MAX)
  35.  
  36.         SET NOCOUNT ON
  37.  
  38.     SET @SQL = 'set @zone_peril =  right(''' + @input_table + ''', 4)
  39.                 set @output_table = ''2018_1_OUTPUT_'''+@cedant+'''_'''+@zone_peril+''''
  40.  
  41.         EXEC(@SQL)
  42.  
  43.         SELECT @SQL =   'Create table'+@output_table+'(column definitions etc...)'
  44.         /*SCRIPT WHICH RUNS DATA INTO @output_table OK */
  45.         END
  46.  
  47. /*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.
  48. 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.
  49. 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