0

I am working on a consolidated SQL script that needs to

  1. Add a datetime column named AdjudicatedDateTime to the WarrantyAdjudicationStaging table.
  2. Add a default date of GETDATE() for new entries into the table
  3. Update old records with NULL values of the AdjudicatedDateTime

The statements seem to run fine alone, but I need them to run together.When I attempt to run them together I get the following error:

Msg 207, Level 16, State 1, Line 27
Invalid column name 'AdjudicatedDateTime'

USE NATesterData
ALTER TABLE WarrantyAdjudicationStaging
ADD AdjudicatedDateTime datetime

USE NATesterData
ALTER TABLE WarrantyAdjudicationStaging
ADD CONSTRAINT DF_WarrantyAdjudicationStaging DEFAULT GETDATE() FOR AdjudicatedDateTime

USE NATesterData
UPDATE WarrantyAdjudicationStaging
SET AdjudicatedDateTime = AuditResults.AdjudicatedDateTime
FROM WarrantyAdjudicationStaging was
INNER JOIN
(
    SELECT MAX(dateTimeRun) AdjudicatedDateTime, was.WarrantyAdjudicationGroupID
    FROM WarrantyAdjudicationStaging was
    INNER JOIN (
        SELECT DISTINCT WarrantyAdjudicationGroupID
        FROM WarrantyAdjudicationStaging
        WHERE WarrantyAuditKey IS NULL
    ) exceptions
    ON was.WarrantyAdjudicationGroupID = exceptions.WarrantyAdjudicationGroupID
    INNER JOIN WarrantyUsageAudit wua
    ON was.WarrantyAuditKey= wua.WarrantyAuditKey
    GROUP BY was.WarrantyAdjudicationGroupID
) AuditResults on was.WarrantyAdjudicationGroupID = AuditResults.WarrantyAdjudicationGroupID
WHERE WAS.AdjudicatedDateTime IS NULL
AND WarrantyAuditKey IS NULL

3
  • 1
    So, what's the problem? What is your question? Why 3 USE statements, when they are define the same database? Commented Jun 22, 2020 at 20:04
  • When I execute the query I get this "Msg 207, Level 16, State 1, Line 27 Invalid column name 'AdjudicatedDateTime'." Commented Jun 22, 2020 at 20:11
  • Put a GO between each block. Remove the unnecessary USE statements. You can combine the first two bocks into one statement... add a column with a default constraint. The syntax is straight-forward. Commented Jun 22, 2020 at 20:23

1 Answer 1

1

Add a GO after the statements to split them in batches

Sign up to request clarification or add additional context in comments.

1 Comment

Note that depending on what is sending the statements GO could cause further errors. GO is not a Transact-SQL Keyword, it's an IDE keyword, used by applications such as SSMS and Azure Data Studio, and CLIs such as sqlcmd and sqlcli.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.