Solved

Query Help

Posted on 2016-09-09
3
40 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
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:ScottPletcher
ScottPletcher 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now