?
Solved

Compare data in two tables

Posted on 2011-09-21
9
Medium Priority
?
202 Views
Last Modified: 2012-05-12
In SQL Server 2008 R2 I have a stored procedure where I can pass in an entire table from VB web page that I want to compare to existing table.  In both tables
col1 is a date column
col2 is a money column

I need to compare the inbound table with the sqlTable

if inbound count(*) > sqlTable count(*) return a fail message

inbound col1 doesn't exist in sqlTable return a fail message
if col1 exists in both tables
inbound.col1 must be < = sqlTable.col1 or return fail message

If nofails...simply return an okee-dokee message
0
Comment
Question by:lrbrister
[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
9 Comments
 
LVL 25

Expert Comment

by:TempDBA
ID: 36574363
So, you have the logic. What you want to ask? can you make it more clear?
0
 

Author Comment

by:lrbrister
ID: 36575169
TempDBA:
how do I cycle through that and grab the first incorrect record and return it with a fail status.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36575268
Please post the schema of the table.
Do you have a Primary Key on the table?  If so indicate what is/are the field(s)
0
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

Author Comment

by:lrbrister
ID: 36581158
acperkins

The user-defined table type is attached.

The inbound table is the same structure

if inbound count(*) > sqlTable count(*) return a fail message

inbound startDatedoesn't exist in sqlTable return a fail message
if startDate exists in both tables
inbound.PmtAmount must be < = tbltypTV5Pmts.PmtAmount or return fail message


The inbound table gets passed in with...

ALTER PROCEDURE [dbo].[sp_z_test] (
                              @parentId      INT
                              ,@tv5Pmts      AS tbltypTV5PMTS readonly)
AS
etc...
CREATE TYPE [dbo].[tbltypTV5Pmts] AS TABLE(
	[EventType] [varchar](25) NULL,
	[StartDate] [datetime] NULL,
	[PmtAmount] [money] NULL,
	[NumPmts] [smallint] NULL,
	[PmtFrequency] [varchar](15) NULL,
	[EndDate] [datetime] NULL,
	[PercentIncrease] [numeric](7, 4) NULL,
	[FixedIncrease] [money] NULL,
	[NumberPmtsBeforeChange] [smallint] NULL
)
GO

Open in new window

0
 
LVL 1

Accepted Solution

by:
millsap_singer earned 2000 total points
ID: 36581887
If you truly want to cycle through then you can use a cursor.  

--Declare your row variables.
DECLARE EventType VARCHAR(25)
DECLARE StartDate DATETIME

--etc.

DECLARE curPayments CURSOR LOCAL STATIC
FOR SELECT EventType, StartDate FROM dbo.tbltypTV5Pmts ORDER BY StartDate

OPEN curPayments

FETCH NEXT FROM curPayments
INTO @EventType, @Start Date

WHILE @@FETCH_STATUS = 0
BEGIN

-- Do your logic here, inserting, returning rows, whatever.


--Get Next row of data.
FETCH NEXT FROM curPayments
INTO @EventType, @Start Date

END --looping through cursor

CLOSE curPayments
DEALLOCATE curPayments

-- do additional work...

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36584332
I am not sure if you saw this:
Do you have a Primary Key on the table?  If so indicate what is/are the field(s)
0
 

Author Comment

by:lrbrister
ID: 36600657
acperkins:
Not usre how the Primary key would help

The inbound table from the table tyle variable has no primary ke

The existing table does...id.  It is an integer seed=1000 increment=1
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36649395
>>Not usre how the Primary key would help<<
I would use it to compare the tables.  But if you know of a better way, that is fine too.
0
 

Author Closing Comment

by:lrbrister
ID: 36708966
THis is a start.  Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

765 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