Solved

Compare data in two tables

Posted on 2011-09-21
9
196 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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