We help IT Professionals succeed at work.
Get Started

INSERT INTO using just created ID

46 Views
Last Modified: 2015-10-21
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

Comment
Watch Question
SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE