1

How I can insert in same row for example I want to insert all these columns data in first row then second and so on. But my query is inserting data when customer name data is complete, status data is inserted after one row of customer number last data.

  CREATE TABLE #tblCustomer 
             (
               CustomerNumber  NVARCHAR(1000),
               Status NVARCHAR (1000),
               CustomerType NVARCHAR (1000)
              )


          INSERT 
            INTO #tblCustomer (CustomerNumber)
          Select c.CustomerNumber 
            From Customer.Customer c
          INSERT 
            INTO #tblCustomer (Status) 
          Select ses.Name 
            From Customer.Customer c 
  Left Outer Join COM.StatusEngine_EntityStatus sees 
               On c.Status = sees.EntityStatusId 
              And sees.EntityId = 'CustomerStatus' 
             Join COM.StatusEngine_Status ses 
               On sees.Status = ses.Status
           INSERT 
             INTO #tblCustomer (CustomerType) 
           select t.Description 
             From Customer.Customer c 
             Join Customer.Type t 
               On c.TypeId = t.pkTypeId

Receiving output:

  0001 null   null
  0002 null   null
  NULL active null
  NULL active null
  NULL null  individual
  NULL null  individual

Expected Output:

 0001 active individual
 0002 active individual
3
  • 2
    Why don't you just use one insert to select them all? Commented Jun 19, 2018 at 9:08
  • @HoneyBadger I have tried but i am not sure hoe to use it with multiple queries Commented Jun 19, 2018 at 9:15
  • You don't need multiple queries, just one for all attributes. Just one select from Customer.Customer with all the joins you need. Commented Jun 19, 2018 at 9:25

4 Answers 4

1

Without knowing more about your tables, you can insert the first records like so...

INSERT INTO #tblCustomer (CustomerNumber)
select  c.CustomerNumber from Customer.Customer c

And then update the remaining columns this way...

UPDATE #tblCustomer 
    set #tblCustomer.Status = c.Status
from Customer.Customer c 
left outer join COM.StatusEngine_EntityStatus sees
    on c.Status = sees.EntityStatusId and sees.EntityId = 'CustomerStatus'
join COM.StatusEngine_Status ses
    on sees.Status = ses.Status
join #tblCustomer temp
    on c.CustomerNumber = temp.CustomerNumber

However doing it like this is really inefficient, you should strive to create an insert that updates all columns in one go.

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

Comments

1

You can do it like this (I have verified the code with the Northwind sample database from Microsoft - I have chosen that one since you can use it for each SQL server version since SQL 2000):

declare @NumberOfItems int = 10;

CREATE TABLE #tblCustomer (
     CustomerNumber  NVARCHAR(1000)
    ,Name NVARCHAR (1000)
    ,CustomerType NVARCHAR (1000))

insert into #tblCustomer
select CustomerNumber, Name, Status from (select top(@NumberOfItems) ROW_NUMBER() OVER(ORDER BY CustomerID) as No, CustomerID as CustomerNumber from Customers) c
left join (select * from (select top(@NumberOfItems) ROW_NUMBER() OVER(ORDER BY ContactName) as No, ContactName as Name from Customers) q2) j1 on c.No=j1.No
left join (select * from (select top(@NumberOfItems) ROW_NUMBER() OVER(ORDER BY ContactTitle) as No, ContactTitle as Status from Customers) q3) j2 on c.No=j2.No

select * from #tblCustomer
drop table #tblCustomer

It will create a column with numbers from 1 to n for each element you want to import and then it joins it together.

The result of this query is:

enter image description here

Note: While this works, it is not the preferred way to do it, because there is no primary key - normally one would look for primary key / foreign key relationships to join the data together.

The way you're intending to fill it puts data together which doesn't necessarily belong together (here each column is sorted and then put together by its row number - i.e. it picks values from rows sorted by its extract column and then putting them together again).

If you have no primary key because you're importing data from other sources, you can add WHERE clauses to create a better connection between the inner and the outer select statements - you can find a nice article which might help you with such kind of subqueries here.

Comments

0

This is untested, however, I believe this is what you're after:

INSERT INTO #tblCustomer (CustomerNumber, [Status], CustomerType))
SELECT  c.CustomerNumber, ses.[Name], t.[Description]
FROM Customer.Customer c
     JOIN COM.StatusEngine_EntityStatus sees ON c.Status = sees.EntityStatusId --Changed to JOIN, as it is turned into a implicit INNER join by the next JOIN
                                            AND sees.EntityId = 'CustomerStatus'
     JOIN COM.StatusEngine_Status ses ON sees.[Status] = ses.[Status];

Note my comment regarding your LEFT OUTER JOIN, in that I've changed it to an INNER JOIN.

4 Comments

This is the requirement i won't change it myself
What is the requirement?
I have mulltiple columns these three are just for demo.1.Use ‘left outer join COM.StatusEngine_EntityStatus sees on c.Status = sees.EntityStatusId and sees.EntityId = 'CustomerStatus' join COM.StatusEngine_Status ses on sees.Status = ses.Status’ 2. Use ‘join Customer.Type t on c.TypeId = t.pkTypeId’
My comment about your LEFT OUTER JOIN doesn't change. if you have a JOIN on a latter table, which relies on the prior LEFT OUTER JOIN, without checking for NULL, then the LEFT OUTER JOIN will be an become an implicit INNER JOIN. "requirements" don't change that, it's a fact, and you'll (probably) get better performance using the correct join.
0

straight forward SQL here:

CREATE TABLE #tblCustomer 
(
    CustomerNumber  NVARCHAR(1000),
    Status NVARCHAR (1000),
    CustomerType NVARCHAR (1000)
)

INSERT INTO #tblCustomer (CustomerNumber, Status, CustomerType)
SELECT DISTINCT 
     c.CustomerNumber, 
     ses.Name, 
     t.Description  
FROM Customer.Customer c
LEFT OUTER JOIN COM.StatusEngine_EntityStatus sees 
    On c.Status = sees.EntityStatusId 
    And sees.EntityId = 'CustomerStatus' 
LEFT OUTER JOIN COM.StatusEngine_Status ses 
    On sees.Status = ses.Status
LEFT OUTER JOIN Customer.Type t 
    On c.TypeId = t.pkTypeId

Comments

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.