• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Sorting table field with sproc

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
fskilnik
Asked:
fskilnik
  • 3
  • 2
1 Solution
 
ZberteocCommented:
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
 
jose_juanCommented:
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
 
fskilnikAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
fskilnikAuthor Commented:
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
 
jose_juanCommented:
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
 
fskilnikAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now