Solved

Compare data in two tables

Posted on 2011-09-21
9
201 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

707 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