Solved

Query Help

Posted on 2016-09-09
3
68 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

627 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