Solved

Sorting table field with sproc

Posted on 2008-10-06
6
150 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
  • 3
  • 2
6 Comments
 
LVL 26

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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