Link to home
Start Free TrialLog in
Avatar of Jacque Scott
Jacque ScottFlag for United States of America

asked on

INSERT INTO using just created ID

I was asked to INSERT records into a couple of tables.  Here is what I am trying to do.

I need to take [SQL3].MattersQACCTNOTES.QBILLINGNOTES and depending on the length of the input I need to split it up and INSERT it into two different fields in [ADR_LIVE].[dbo].TBM_TEXT.  

Once I do this I need that newly created TEXT_ID and INSERT it into [SQL3].MattersQACCTNOTES.Nar_Text_ID.

1) Am I using SUBSTRING correctly?  Is there a better way to do the same thing?
2) Once I INSERT into the TBM_TEXT table how do I get that ID and INSERT it into [SQL3].MattersQACCTNOTES.Nar_Text_ID?  I would like to use this same query if possible.

Please note that there are two different DB's.


INSERT INTO [ADR_LIVE].[dbo].TBM_TEXT
(TEXT_ID, TXT1, TXT2, LAST_MODIFIED)

SELECT NEWID(),  SUBSTRING(mqa.QBILLINGNOTES, 1, 250), SUBSTRING(mqa.QBILLINGNOTES, 251, 250), GETDATE()
FROM [ADR_LIVE].[dbo].HBM_MATTER hm
LEFT JOIN [SQL3].MattersQACCTNOTES mqa ON hm._ProLaw_FK = mqa.Matters
JOIN [ADR_LIVE].[dbo].TBM_MATTER tm ON hm.MATTER_UNO = tm.MATTER_UNO
JOIN [ADR_LIVE].[dbo].TBM_TEXT tt ON tm.NAR_TEXT_ID = tt.TEXT_ID 
WHERE ((mqa.QBILLINGNOTES IS NOT NULL OR mqa.QBILLINGNOTES <> '') AND tt.txt1 IS NULL)
OR (tt.txt1 + tt.txt2)  <> mqa.QBILLINGNOTES)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacque Scott

ASKER

Here is a some data.

BEFORE:
Table mqa:
mqa.Matters              Nar_Text_ID                  QBillingNotes
555DDD23ILI                  NULL                        Client will make payments
22DDD555ILI                  NULL                        Bill the family
1222233IIILLI                  NULL                        The don't accept bills

Table tt does not have any data for the above


AFTER:

table tt:
tt.TEXT_ID                        tt.txt1
    251                         Client will make payments
   252                           Bill the family
    253                          The don't accept bills

Table mqa:
mqa.Matters              Nar_Text_ID                  QBillingNotes
555DDD23ILI                  251                           Client will make payments
22DDD555ILI                  252                           Bill the family
1222233IIILLI                  253                          The don't accept bills
This question doesn't need to be answered.  I found out the fields are not the fields we use.
This doesn't need to be answered.