Solved

Compare data in two tables

Posted on 2011-09-21
9
193 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now