Avatar of WEhalt
WEhalt
Flag for United States of America asked on

Define Field in an INSERT into CREATE Table Query

What is the proper syntax to add fields with an create/insert query like this.  I would like to add them for later population.  I am trying to avoid inserting dummy data.

  SELECT CLID,
    LOB,
    @WEEK AS WEEK_ID,
    VARCHAR (10) AS CLAIM_LOC ,
    INT as DAYS_IN_PROC
  INTO #CLAIMS_BY_WEEK        
  FROM CMC_CLCL_CLAIM CLCL
Microsoft DevelopmentMicrosoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
Jim Horn

By 'add fields' do you mean create new fields?  

If yes,

ALTER TABLE #CLAIMS_BY_WEEK
ADD CLID whatever, LOB whatever, WEEK_ID int, CLAIM_LOC varchar(10), DAYS_IN_PROC int

If no, you're trying to insert data, then...

INSERT INTO #CLAIMS_BY_WEEK (CLID, LOB, WEEK_ID, CLAIM_LOC, DAYS_IN_PROC)
SELECT CLID, LOB, @WEEK, 'No idea what you mean here by varchar(10),', 'Same here'
FROM CMC_CLCL_CLAIM
WEhalt

ASKER
I believe I have seen a way to declare the columns with their types in the select statement.  That is what I mean by these lines

    VARCHAR (10) AS CLAIM_LOC ,
    INT as DAYS_IN_PROC
WEhalt

ASKER
I got the following to work, but I really thought there was a way to embed the declaration right into the INSERT statement.

DECLARE @CLAIM_LOC VARCHAR(10)
DECLARE @CLAIM_PROC_DAYS_GRP VARCHAR(50)

  SELECT CLCL.*,
    @CLAIM_LOC AS CLAIM_LOC,
    @CLAIM_PROC_DAYS_GRP AS CLAIM_PROC_DAYS_GRP
  INTO #CLAIMS_BY_WEEK        
  FROM CLCL
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
ralmada

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jim Horn

Thanks for the split.  Good luck with your project.  -Jim