Solved

Sorting table field with sproc

Posted on 2008-10-06
6
152 Views
Last Modified: 2012-05-05
Hi there!

In the sproc below:

---------------------------------------------------------------------------
ALTER PROCEDURE [dbo].[insProperPosition_sproc]
@itemPosition int,
@Field1 int,
@Field2 int
AS
IF @ItemPosition > 0
      
    UPDATE testTable
      SET ItemPosition = ItemPosition + 1
      WHERE ItemPosition >= @ItemPosition
---------------------------------------------------------------------

I would like to insert a restriction, for instance, I want to re-numerate the itemPositions of all the testTable records in with the Field2 equals the number 3 (and only these records, sure), and the renumeration should be ORDERed (ASC) by the (PK) Field1...

Could you please show to me how to adapt the procedure given to satisfy this restriction?  Thanks!
0
Comment
Question by:fskilnik
[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
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Zberteoc
ID: 22650282
Can you give some data example of what you have and what you want to achieve?

What is with the other two parameters of the proc?
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22650441
Hi fskilnik,

your execution not like a good idea. What not seach other solution to your problem? (what is your problem? really)

Nevertheless, use a intermediate table, like this...

Good luck!

DECLARE @start int -- start incremental value
               -- you must be sure all future f1 not are in use!!!
 
-- I suppuse f1 are your PK
-- Then
 
BEGIN TRANSACTION -- is needed
 
DECLARE @vt TABLE (
   ii int identity(0,1),
   f1 int
)
INSERT INTO @vt ( f1 )
SELECT f1
FROM YourTable
WHERE Your_Condition
ORDER BY Your_Order
 
UPDATE YourTable
SET f1 = @start + ii
FROM YourTable a
JOIN @vt b
ON a.f1 = b.f1
 
COMMIT TRANSACTION

Open in new window

0
 

Author Comment

by:fskilnik
ID: 22690242
Hi Zberteoc and jose_juan,

Sorry for the (week-long) delay.

>Can you give some data example of what you have and what you want to achieve?
>What is with the other two parameters of the proc?

>>your execution not like a good idea.
>> what is your problem? really

You are both right, my question was poorly created. The 2 parameters included (and not used) are there just to ilustrate the ones that would be needed to satisfy the condition explained after the code, that is:
 
... I want to re-numerate the itemPositions of all the testTable records in with the Field2 equals the number 3 (and only these records, sure), and the renumeration should be ORDERed (ASC) by the (PK) Field1...

I guess jose_juan´s code may be what I need, but I have no experience dealing with temporary (intermediate) tables, therefore I will give you (both) the REAL fields and ask my question in an understandable fashion...

01. My  TestTable is the created by the snippet attached.
02. Let us consider the following records already present at the TestTable:

tbTestID //  Material ID  // PageNumber   // PagContent     <----- Name of Fields

Value of the corresponding fields:

1      1      1      abc
2      1      2      def
3      2      1      ghi
4      2      2      jkl
5      1      3      mno
6      1      5      stu
7      1      4      pqr
9      2      3      vxz

Now what I need?  A sproc ("based" on the sproc given, or not...)  say   "insProperPosition(i, j)" such that
i is the (integer) that asks for the (PageNumber) position to be included (more below) and j is the MaterialID that should be considered. The sproc should do as below:

Example: if I run  insProperPosition(2, 1) that means that I want the store procedure to "open" the PageNumber = 2 of the MaterialID = 1 in the sense that the table above given should now be:

           MatID      PageNumb
1      1      1      abc
2      1      THREE      def
3      2      1      ghi
4      2      2      jkl
5      1      FOUR      mno
6      1      SIX      stu
7      1      FIVE      pqr
9      2      3      vxz

In other words, the MaterialID= 1 and PageNumber =2 has already no records, because I´ll be able to insert it now without interrupting the enumeration. Please note that the MaterialID =2 records were not altered and that the MaterialID = 1 records where NOT altered when int < 2 was considered (sure, just the PageNumber = 1 therefore) and ALL MaterialID = 1 records with PageNumber > = 2 where 1 units ahead added, I mean, PageNumber 2 is now PageNumber 3 , PageNumber 3 (previous) is now PageNumber 4, etc...

I hope I got understood!
Thanks a lot for your time and expertise, fskilnik.







SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[testTable](
	[tbTestID] [int] IDENTITY(1,1) NOT NULL,
	[MaterialID] [int] NOT NULL,
	[PageNumber] [int] NOT NULL,
	[PgContent] [nvarchar](200) COLLATE SQL_Latin1_General_CP850_CI_AI NOT NULL
) ON [PRIMARY]

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:fskilnik
ID: 22690290
Of course I put THREE where should be 3 etc, it´s just for the sake of emphasizing where and how the table was altered.

(Of course both parameters i and j are always "reasonable"... this sproc is for myself only, therefore I will not need error-routines... for instance, if I put i = 0, I would like the sproc not to run... just that...)

0
 
LVL 5

Accepted Solution

by:
jose_juan earned 500 total points
ID: 22709181
Hi fskilnik,

uhm... I think your problem have trivial solution, if your initial data is ordered then only updating PageNumbers to get displacement is needed.

See code (complete example code).

Regards.

GO
CREATE TABLE testTable (
        tbTestID int IDENTITY(1,1) NOT NULL primary key, -- Use PK always!
        MaterialID int NOT NULL,
        PageNumber int NOT NULL,
        PgContent nvarchar(200)
)
GO
-- Use unique keys to set restriction! prevent errors!
CREATE UNIQUE INDEX UK_testTable ON testTable ( MaterialID ASC, PageNumber ASC )
GO
 
-- Example data
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 1, 1, 'abc' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 1, 2, 'def' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 2, 1, 'ghi' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 2, 2, 'jkl' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 1, 3, 'mno' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 1, 5, 'stu' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 1, 4, 'pqr' )
INSERT INTO testTable ( MaterialID, PageNumber, PgContent ) VALUES ( 2, 3, 'vxz' )
 
GO
SELECT * FROM testTable ORDER BY MaterialID, PageNumber
 
GO
CREATE PROCEDURE sp_insProperPosition
	@pN int,
	@mID int
AS
	SET NOCOUNT ON
 
	UPDATE	testTable
	SET	PageNumber	= PageNumber + 1
	WHERE	MaterialID	= @mID
	AND	PageNumber	>= @pN
GO
 
EXEC sp_insProperPosition 2, 1
 
GO
SELECT * FROM testTable ORDER BY MaterialID, PageNumber
 
GO
 
DROP PROCEDURE sp_insProperPosition
DROP TABLE testTable

Open in new window

0
 

Author Comment

by:fskilnik
ID: 22709888
Hi jose_juan,

Thanks for the really complete solution. It´s perfect and, yes, pretty simple (after you made it, for sure)!!

I hope to see you in my future difficulties.    :)

Best Regards,
fskilnik.
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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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