studio NETSOULS

Applying Web To Your Business

Many of us have faced a problem where we need to compare historical records for a particular set of records to ensure that records have been saved in the correct order.

This could be for comparisons of dates or numbers or anything.  The need is to ensure that the historical records have been saved in the correct order. Using Cross joins, we can identify records that have been stored out of order for a subset of records.

Let’s say I have table in which there are three fields:

EmployeeId
Version
CreateDate

Here, EmployeeId is an integer field and is our primary ID. Version is also an integer field,considering this as a secondary ID. So, in totality EmployeeId and Version will form the Composite Key. There can be many versions of the same EmployeeId and each Version would have a corresponding CreateDate.

Our problem is to compare the CreateDate of all the versions of a particular employee on an iterative basis using a single query to ensure the CreateDate are in order based on the Version for a particular employee.

The below scripts will help you understand the problem and its solution.  I am using a new table, adding some data and then using the query to determine which records are out of order.

   1: CREATE TABLE Employees(
   2:      EmployeeID INT        NOT NULL,
   3:      Version    INT        NOT NULL,
   4:      CreateDate DATETIME   NULL,
   5: CONSTRAINT pk_Employees PRIMARY KEY CLUSTERED 
   6: (
   7:      [EmployeeID] ASC,
   8:      [Version] ASC
   9: ))
  10: GO
  11: INSERT INTO Employees VALUES(1,0,'03/10/2000')
  12: INSERT INTO Employees VALUES(1,1,'03/16/2000')
  13: INSERT INTO Employees VALUES(1,2,'03/19/2000')
  14: INSERT INTO Employees VALUES(1,3,'03/18/2000')
  15: INSERT INTO Employees VALUES(1,4,'03/17/2000')
  16: INSERT INTO Employees VALUES(2,0,'02/10/2000')
  17: INSERT INTO Employees VALUES(2,1,'02/11/2000')
  18: INSERT INTO Employees VALUES(2,2,'02/18/2000')
  19: INSERT INTO Employees VALUES(3,0,'03/25/2000')
  20: INSERT INTO Employees VALUES(3,1,'03/23/2000')
  21: INSERT INTO Employees VALUES(3,2,'03/26/2000')
  22: INSERT INTO Employees VALUES(3,3,'03/30/2000')
  23: INSERT INTO Employees VALUES(4,0,'08/19/2000')
  24: INSERT INTO Employees VALUES(4,1,'08/20/2000')
  25: INSERT INTO Employees VALUES(4,2,'08/23/2000')
  26: INSERT INTO Employees VALUES(4,3,'08/24/2000')
  27: GO
  28: SELECT *,
  29:      (SELECT 
  30:           CASE WHEN (SUM(CASE WHEN B.CreateDate<C.CreateDate THEN 0 
  31:                ELSE 1 
  32:                END))>=1 THEN 1 
  33:           ELSE 0 
  34:           END AS IsNotProper 
  35:      FROM Employees B CROSS JOIN 
  36:           Employees C
  37:      WHERE B.EmployeID=A.EmployeID 
  38:           AND C.EmployeID=A.EmployeID 
  39:           AND B.Version<>C.Version 
  40:           AND B.Version<C.Version) 
  41:      AS [OutOfOrder] 
  42: FROM Employees A 

In the output it will display all the records with a flag called OutOfOrder. If the flag is 1 that means there is a problem with the record order. Otherwise, the EmployeeID’s versions are in proper order.

In the above example you can see that EmployeeID 1 has two records that are out of order.  The 2003-03-18 record came before the 2008-03-17 record, so the query picked these up as being out of order.

Query Result

So, instead of using loops and cursors to determine if there is an issue it handles everything in a single query.

  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Sometimes you may want to pass a comma separated string as an argument to stored procedure or SQL function and parse within the procedure. This sample code shows how to parse a string based any delimiter you specify.

CREATE FUNCTION dbo.CreateInlineTable (
    @Param AS VARCHAR(7999), -- Deliminated string
    @Deliminator AS VARCHAR(10) -- Deliminator
    )   RETURNS TABLE
AS
    RETURN ( SELECT Substring(@Deliminator + @Param + @Deliminator, Number + LEN(@Deliminator), 
                    CHARINDEX(@Deliminator, @Deliminator + @Param + @Deliminator, Number + LEN(@Deliminator)) - Number - LEN(@Deliminator) ) AS PARAM
               FROM InlineNumbers Numbers
              WHERE Number <= LEN(@Deliminator + @Param + @Deliminator) - LEN(@Deliminator)
                    AND Substring(@Deliminator + @Param + @Deliminator, Number,LEN(@Deliminator)) = @Deliminator)
 
GO

More...

  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

If you are using SQL Server 2005 and not able to connect to it remotely, then check out this useful blog post.

  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Let's create and populate some tables so we have some data to work with. Assume you have a sales effort management (SEM) system that allows you to track the number of sales calls made on a potential client. A sales call is not a phone call, but a get together such as lunch or another type of person-to-person meeting. One of the things the VP of Sales wants to know is how many of his sales personnel participate in a call. The following tables allow you to track this information.

   1: CREATE TABLE Employees
   2: (
   3:     Emp_UniqueID smallint PRIMARY KEY,
   4:     Emp_FName varchar(30) NOT NULL,
   5:     Emp_LName varchar(30) NOT NULL,
   6: )
   7: go
   8:  
   9: CREATE TABLE SalesCalls
  10: (
  11:     SalCal_UniqueID smallint PRIMARY KEY,
  12:     SalCal_Desc varchar(100) NOT NULL,
  13:     SalCal_Date smalldatetime NOT NULL,
  14: )
  15: go
  16:  
  17: CREATE TABLE SalesCallsEmployees
  18: (
  19:     SalCal_UniqueID smallint NOT NULL,
  20:     Emp_UniqueID smallint NOT NULL,
  21: )
  22: go

More...

  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

When you restore a Microsoft SQL Server database on a different machine, you cannot access the database until you fix the permissions.

The problem is that the user in the database is an "orphan". This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-speak) that has to match as well.

This used to be a pain to fix, but currently (SQL Server 2000, SP3 and higher) there is a stored procedure that does.

You should execute the stored procedure as a database admin, with the restored database selected

--First, make sure that this is the problem. This will lists the orphaned users:
EXEC sp_change_users_login 'Report' 
 
--If you already have a login id and password for this user, fix it by doing: 
EXEC sp_change_users_login 'Auto_Fix', 'user' 
 
--If you want to create a new login id and password for this user, fix it by doing: 
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password' 
  • Currently 0 /5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

About Us

studio NETSOULS is a complete IT services company, offering strategy, design, development and implementation of the total solution for your web and IT initiatives. The solutions we provide, enables businesses to leverage leading edge technology to gain sustainable competitive advantages in today's marketplace.

We specialize in designing, developing and deploying the next generation of IT solutions including e-business solutions Read more...

Tags

This will be shown to users with no Flash or Javascript.

Contact Us

My status

Quote of the Day

"Do you see a man wise in his own eyes? There is more hope for a fool than for him."

- Proverbs 26:12

NutritionVista

www.NutritionVista.com

Archives


Advertisements


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Log in