Solved

Query Help

Posted on 2016-09-09
3
64 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 250 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

742 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