Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query Help

Posted on 2016-09-09
3
Medium Priority
?
74 Views
Last Modified: 2016-09-13
I have a table with 5 columns.   i am trying to insert data into this table, but one column needs a value that is derived from another table and another column needs to have the next highest available interger in that column added.

the 5 columns are

CalculateDateTime (datetime) -  value needed is a constant null
AccountStartBalance(interger)- value needed is a constant integer with a value of 0
AccountServiceKey - The value needs to be the next highest available in that column value incremented by the value of 1
ServiceOption(nvarchar) - value needs to be a blank or no value but not null either
Accountkey -  This needs to be derived from another table.....   Select Accountkey from billingmaster where accountnumber = '999999999'

the result from the last column query would be '11111'
I dont have a clue of how to construct the insert statement but i am guessing it would be.

insert into billingsecondary
values (NULL,0,I need help, I need help, I need help);
0
Comment
Question by:jamesmetcalf74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Expert Comment

by:Bill Bach
ID: 41791885
Would this work?

insert into billingsecondary
values (NULL,0,1+(SELECT TOP 1 AccountServiceKey FROM billingsecondary), '', (Select TOP 1 Accountkey from billingmaster where accountnumber = '999999999'));
0
 
LVL 1

Accepted Solution

by:
Brad Featherstone earned 1000 total points
ID: 41791953
Be lazy - There is a way to have SQL Server auto magically do a lot of the work for you.

It all depends on how you define the columns of the table:


declare table billingsecondary
(
      CalculateDateTime datetime default null      
,      AccountStartBalance int default 0
,      AccountServiceKey bigint identity(1,1)
,      ServiceOption nvarchar(<<whatever length>>) default ''
,      Accountkey <<notype>>
);
go

-- look up the account key
declare @Accountkey <<notype>>;
Select @Accountkey = Accountkey from billingmaster where accountnumber = '999999999';

-- push in a record
insert into billingsecondary (ServiceOption, Accountkey) values ('Shoe Super Shine', @Accountkey);

Since yo did not specifically load the following columns, they will contain either the defined default value or, in the case of the identity column, the next integer value
  • CalculateDateTime
  • AccountStartBalance
  • AccountServiceKey



If this is the first ever insert into the table, the record will contain
  • CalculateDateTime: NULL
  • AccountStartBalance: 0
  • AccountServiceKey: 1
  • ServiceOption: Shoe Super Shine
  • Accountkey: <<what ever value you looked up>>

The second insert into the table, the record will contain
  • CalculateDateTime: NULL
  • AccountStartBalance: 0
  • AccountServiceKey: 2
  • ServiceOption: Shoe Super Shine
  • Accountkey: <<what ever value you looked up>>
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 41794627
You'll want to lock the table as you get the max value, otherwise if two INSERTs ran at the same time, they could both get the same AccountServiceKey number:

SELECT NULL AS CalculateDateTime, 0 AS AccountStartBalance,
    AccountServiceKey_Max AS AccountServiceKey,
    '' AS ServiceOption, bm.AccountKey AS AccountKey
FROM dbo.billingsecondary bs
CROSS JOIN (
    /* lock the table to guarantee that the max value is not read at the same time by diff queries */
    SELECT MAX(AccountServiceKey) AS AccountServiceKey_Max
    FROM dbo.billingsecondary WITH (TABLOCK)
) AS cj1
LEFT OUTER JOIN dbo.billingmaster bm ON bm.accountnumber = '999999999'
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question