Hi All,I am trying to execute the following query in SQL SERVER 2012.SELECT * FROM OPENROWSET ('SQLNCLI', 'Server=DBName;UID=newuser;PWD=newuser;', 'exec TESTDB.[dbo].[DeleteTableData]')I am trying to delete a record from a Remote DB Server. The query gets executed without any issues, but does not delete the record. Also it does not throw an error.SP Created:CREATE PROCEDURE [dbo].[DeleteTableData] ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; delete from [TestDB].[dbo].[Table1] where id=1 select 1 as iREsultENDKindly help to resolve this issue.Regards,Geeta.
↧
OpenRowSet does not delete records in Remote DB
↧
for clause (transact-sql) browse
from http://technet.microsoft.com/en-us/library/ms173812.aspx[quote]The browse mode does not support the unique index if the following conditions are true:You try to access the data from SQL Server tables in browse mode by using a SELECT query that involves an outer join statement.A unique index is defined on the table that is present on the inner side of an outer join statement.[/quote]examples given is as follow[code="sql"]CREATE TABLE tleft(c1 INT NULL UNIQUE) ;GO CREATE TABLE tright(c1 INT NULL) ;GOINSERT INTO tleft VALUES(2) ;INSERT INTO tleft VALUES(NULL) ;INSERT INTO tright VALUES(1) ;INSERT INTO tright VALUES(3) ;INSERT INTO tright VALUES(NULL) ;GOSET NO_BROWSETABLE ON ;GOSELECT tleft.c1 FROM tleft RIGHT JOIN tright ON tleft.c1 = tright.c1 WHERE tright.c1 <> 2 ;c1----NULLNULL[/code]My question is how do we know the current value of NO_BROWSETABLE?from http://technet.microsoft.com/en-us/library/ms190356.aspxthere's no mentioned of no_browsetable.also how do we verify this fact[quote]The browse mode does not support the unique index if the following conditions are true:You try to access the data from SQL Server tables in browse mode by using a SELECT query that involves an outer join statement.A unique index is defined on the table that is present on the inner side of an outer join statement.[/quote]thanks a lot!
↧
↧
To get employee hierarchy
Hi[code="sql"]-- Create an Employee table.CREATE TABLE dbo.MyEmployees( EmployeeID smallint NOT NULL, FirstName nvarchar(30) NOT NULL, LastName nvarchar(40) NOT NULL, Title nvarchar(50) NOT NULL, DeptID smallint NOT NULL, ManagerID int NULL, CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC) );-- Populate the table with values.INSERT INTO dbo.MyEmployees VALUES (1, N'Ken', N'Sánchez', N'Chief Executive Officer',16,NULL),(273, N'Brian', N'Welcker', N'Vice President of Sales',3,1),(274, N'Stephen', N'Jiang', N'North American Sales Manager',3,273),(275, N'Michael', N'Blythe', N'Sales Representative',3,274),(276, N'Linda', N'Mitchell', N'Sales Representative',3,274),(285, N'Syed', N'Abbas', N'Pacific Sales Manager',3,273),(286, N'Lynn', N'Tsoflias', N'Sales Representative',3,285),(16, N'David',N'Bradley', N'Marketing Manager', 4, 273),(23, N'Mary', N'Gibson', N'Marketing Specialist', 4, 16);[/code]here is the query to display all the employees [code="sql"]WITH DirectReports (ManagerID, EmployeeID, Title, Level)AS( SELECT e.ManagerID, e.EmployeeID, e.Title, 0 AS Level FROM dbo.MyEmployees AS e WHERE ManagerID IS NULL UNION ALL SELECT e.ManagerID, e.EmployeeID, e.Title, Level + 1 FROM dbo.MyEmployees AS e INNER JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID)SELECT d.ManagerID, d.EmployeeID, d.Title, LevelFROM DirectReports as d--WHERE d.EmployeeID = 285 OR Level = 0GO[/code]What i want when i pass employee id = 285 then i want to display the result of employeID = 273,1,285His manager and manger's manager and his[code="plain"]ManagerID EmployeeID Title LevelNULL 1 Chief Executive Officer 01 273 Vice President of Sales 1273 285 Pacific Sales Manager 2[/code]thank you
↧
SQL Report Builder
Hi,Not sure where to put this...Im working in SQL Report Builder (sorry dont know which version) and am having trouble with calculated fields.Ive got 2 datasets - one with some tenancy information in it (DataSet1) and the other with address data in it (DataSet2)Ive got a calculated field that ive called "DaysVoid" which is essentially one date subtracted by another.I want to work out the total sum of all of the "DaysVoid" so have set up another calculated field which is called "TotalDaysVoid" //=sum(DaysVoid)\\ but when I run my query, I get the following error;//The Value expression for the textrun ‘Textbox34.Paragraphs[0].TextRuns[0]’ contains an error: [BC30451] Name 'DaysVoid' is not declared.\\Can anybody help?
↧
Behaviour of CONVERT with GETDATE() !!!
[size="1"]select dateformat from sys.syslanguages where name = @@LANGUAGE --[b]mdy[/b]select getdate() --[b]'2013-11-11 16:00:04.960'[/b][/size]Dear All,Above are the stats at the time of creating this post.Below are the some select statements and their outputs where I'm trying to convert a date value into Varchar and then again converting output into date to get only the date part (time part will become '00:00:00' or trimmed)Can somebody please explain, why we have different behaviors for similar type of select statements !!![size="1"]select CONVERT(DATETIME, CONVERT(VARCHAR(12), getdate())) --[b]'2013-11-11'[/b]select CONVERT(DATETIME, CONVERT(VARCHAR(10), '2013-11-11 16:00:04.960')) --[b]'2013-11-11'[/b]select CONVERT(DATETIME, CONVERT(VARCHAR(10), getdate())) --[b]'Conversion failed when converting date and/or time from character string.'[/b]select CONVERT(DATETIME, CONVERT(VARCHAR(12), '2013-11-11 16:00:04.960')) --[b]'Conversion failed when converting date and/or time from character string.'[/b]select CONVERT(VARCHAR(12), getdate()) --[b]'Nov 11 2013 '[/b]select CONVERT(VARCHAR(10), '2013-11-11 16:00:04.960') --[b]'2013-11-11'[/b]select CONVERT(VARCHAR(10), getdate()) --'Nov 11 201'[/b]select CONVERT(VARCHAR(12), '2013-11-11 16:00:04.960') --[b]'2013-11-11 1'[/b][/size]
↧
↧
Opening an ADODB recordset from SQL Server 2012 Stored Procedure
I have a large SQL Server database with hundreds of complex stored procedures, many of which execute a Select statement from a local temporary table. Here is a simplified example.CREATE PROCEDURE [dbo].[usp_getMyPremiums]@PolicyID intASCREATE TABLE #tblPremiums (Premium money NULL, MemberPremium money NULL) ON [PRIMARY];INSERT INTO #tblPremiums (Premium, MemberPremium)SELECT SUM(LiabilityPremium), SUM(MemberLiabilityPremium)FROM tblACCTChargeGROUP BY PolicyIDHAVING (PolicyID = @PolicyID)SELECT Premium, MemberPremiumFROM #tblPremiumsNote that in this version of the stored procedure we first Insert values into a local temporary table and then selects the values from that table.A different version of this stored procedure that does not use a temporary table is as follows:CREATE PROCEDURE [dbo].[usp_getMyPremiums]@PolicyID intASSELECT SUM(LiabilityPremium) AS Premium, SUM(MemberLiabilityPremium) AS MemberPremiumFROM tblACCTChargeGROUP BY PolicyIDHAVING (PolicyID = @PolicyID)Both versions return the same values when executed from SQL Server Management Studio's with EXECUTE usp_getMyPremiums 11407This is true with the database in both SQL Server 2008 and SQL Server 2012.I call on such stored procedures to open ADODB recordsets from MSAccess 2010 applications. Typical VBA code is as follows:Public Function getMyPremiums(PolicyID As Long) As StringOn Error GoTo Err_getMyPremiums Dim usp As String Dim rst As ADODB.Recordset Set rst = New ADODB.Recordset rst.CursorLocation = adUseServer usp = "usp_getMyPremiums " & PolicyID Call CheckConnection rst.Open usp, gCnn, adOpenForwardOnly, adLockReadOnly, adCmdText Debug.Print rst!Premium Debug.Print rst!MemberPremium Debug.Print "getMyPremiums executed without error" Exit_getMyPremiums:On Error Resume Next rst.Close Set rst = Nothing Exit FunctionErr_getMyPremiums: MsgBox "The application encountered unexpected " & _ "error # " & Err.Number & " with message string " & _ Chr(34) & Err.Description & Chr(34) & ".", _ vbExclamation, "getMyPremiums" Resume Exit_getMyPremiums End FunctiongCnn above is a connection string to the SQL Server database.CheckConnection above verifies that the connection is open. Now, here is my problem:When I call such a function, and the stored procedure is in a SQL server 2008 database, both versions of the above stored procedure work properly.When I call such a function, and the stored procedure is in a SQL server 2012 database, only the second version of the above stored procedure works properly. The first version which relies on a temporary table returns an error # 3704 "Operation is not allowed when the object is closed". This error occurs on the first statement that follows the rst.open statement.Can anyone explain why the first version works with SQL Server 2008 but not with SQL Server 2012 when called by the rst.open statement in the VBA code? I set the database compatibility level to SQL Server 2008; but that didn't help.As I stated earlier, I have many such complex stored procedures in a large database, and I hate the thought of having to revise them all especially since my client has not yet moved to SQL Server 2012.Thanks in advance for any help you can give me.
↧
Proc with input paramaters does not compile
I have a SQL 2008 R2 proc which compiles & executes fine. It receives input variables to execute. This proc no longer compiles in SQL 2012. The Proc is invoked like this:EXEC MyDB.dbo.sp_MyProc @myVar_1 ='value_x', @myVar_2 = 1.0 @myVar_3 = 1, @myVar_4 = 5[b][i]How should I revise this code to compile properly[/i][/b]?CREATE PROCEDURE [MyDB].[dbo].[sp_MyProc] @myVar_1 nvarchar(256) = N'' ,@myVar_2 FLOAT=10 ,@myVar_3 BIT=0 ,@myVar_4 INT=30ASBEGIN SET NOCOUNT ON; DECLARE @myVar_5 varchar(50) ,@myVar_6 nvarchar(max) ,@myVar_7 nvarchar(255) SET @myVar_5 = '1' SET @myVar_6 = 'xyz' SET @myVar_7 = 'abc' ... Thx in advance!
↧
stored procedure
Hii,I am trying to write a stored procedure to check whether all the columns of different tables are filled or not please help resolve problem.
↧
More than one character
Hi there, In SQL2000 I used to use the following in my SELECT statement - [code="sql"]and len ([Initials]) > 1[/code]However it doesn't appear to work anymore in 2012.Basically it will look at the Initials field and pull back anything that has more than one character. So only one character should ever exist and I want to know where there are more than one. Is LEN the correct command to use?Thanks
↧
↧
Return the last version of set of records
Hello all, I have a query I am working on where an orders table has a version column for each line in the order and the order number is a column as well. I am trying to retrieve the line numbers of the orders but only showing their last version. I tried it using TOP 1 and MAX function but it isn't working. Can someone suggest a way to get the desired result set?Below is a sample of the DDL[code="sql"]CREATE TABLE [dbo].[tempSalesOrder]( [DocumentNum] [varchar](20) NOT NULL, [LineNum] [int] NOT NULL, [VersionNum] [int] NOT NULL, [CustomerNum] [varchar](20) NOT NULL,) GOinsert into [dbo].[tempSalesOrder]( [DocumentNum],[LineNum], [VersionNum], [CustomerNum])Values( 'SO-1234', '1', '1', '108'),( 'SO-1234', '1', '2', '108'),( 'SO-1234', '2', '1', '108'),( 'SO-1234', '2', '2', '108'),( 'SO-1234', '2', '3', '108'),( 'SO-1234', '2', '4', '108'),( 'SO-1234', '3', '1', '108')Select * from [dbo].[tempSalesOrder];[/code]Below is a sample of the desired result set[code="plain"][DocumentNum],[LineNum], [VersionNum], [CustomerNum]SO-1234 1 2 108SO-1234 2 4 108SO-1234 3 1 108[/code]Even though I have 7 records for the particular order I only want to see the last version which would give me 3 records. Any suggestions would be appreciated. Thanks.
↧
sql query for ssrs report
I have the following report I need to generate . I am new to SSRS.[code="plain"] Number of Stores with June July Aug 0 sales 12 34 32 1 to 9 Sales 12 34 45 10+ sales 15 45 54[/code]The tables I have are as follows :[code="other"]Store : YEAR MONTH StoreName 2013 10 ABC2013 10 DEF2013 09 JKL 2013 06 FGH[/code][code="other"]Store Sales : (Contains only stores whose sales are > 0)YEAR MONTH StoreName NumberOfSales2013 10 ABC 32013 09 JKH 142013 10 FRH 9[/code]I am not really sure what I need to do to get the report in the above format ? I can write a query for sales in a single month , but how do I write a query to get the report for all 3 months ? Or is there a better way to do these reports using ssrs ?If you can direct me on what to do it will be great ?
↧
Split function in sql
Hi ,I need the store procedure which i need to get the comma seperator values result in different columns and pipe line seperator in in next rowi am passing the data through parameter values are suppose (4,2,true,true,true,false|4,2,true,true,true,true)pipeline seperator is in next row1 4 2 TRUE TRUE FALSE FALSE2 4 2 TRUE TRUE TRUE FALSE then the result set i need to update in the database which base on primary key first column is id column which is my primary key Can any one help me :-)
↧
@@variables
Is there a central location where all the @@ variables are described? What I am hoping for is a variable that contains the currently executing line number of the script. Currently I hard code that number for my debug, but of course as lines are added deleted those number drift.I would add that searching for @@ in sql server does not seem to do the trick, and if I search for some known quantity (@@ROWCOUNT, etc.) I get the descriptions for that particular one, but no reference to the rest.
↧
↧
key index of full text index
Need to change from a non cluster index to a PK index. The original non cluster index is used in a full text index.Is there a way to simply alter the key index of a full text index to a different index?
↧
Calling SP with optional parameters - assignment not happening correctly...
Hi All...I have a question regarding calling SP with optional parameters...I have created a SP with definition like this[code="sql"]CREATE PROCEDURE [dbo].[GetPerson] @RecordNumber VARCHAR(16) = NULL , @PerNumber VARCHAR(9) = NULL , @PerType VARCHAR(2) = NULL , @ActiveOnly BIT =null , @ActiveDate datetime =null[/code][b]I need to be able to call this SP in two ways[/b][b]either by passing RecordNumber i.e. first parameter[/b][b]or by passing PerNumber/PerType combination.. i.e. 2nd parameter and 3rd paramater[/b]The last 2 paramaters are applicable in both callsBut when I call this SP like this[code="sql"]exec [dbo].[GetPerson] '123',1,'10/10/2013' [/code][b]it does assignment like this[/b]RecordNumber = '123'PerNumber = 1PerType = 10[b]instead it should be assigning like this:[/b] RecordNumber = '123'Active= 1ActiveDate= 10/10/2013I don't get any results....Can anyone please suggest something on how can I call or take care of optional parameters so that i calls properly based on parameters passed....[code="sql"] CREATE TABLE [Person]( [RecNo] [char](16) NOT NULL, [PerNo] [char](9) NOT NULL, [Pertype] [char](2) NOT NULL, [Active] [int] NULL, [ActiveDate] [datetime] NULL) INSERT INTO [Person]SELECT '123','11','01',1,'2013-10-10'UNION ALLSELECT '345','11','02',1,'2013-11-10'UNION ALLSELECT '456','12','01',1,'2013-9-10'UNION ALLSELECT '789','12','02',1,'2013-8-10'UNION ALLSELECT '234','13','01',1,'2013-9-9'UNION ALLSELECT '678','13','02',1,'2013-8-8' UNION ALLSELECT '1234','15','01',0,'2013-9-9'UNION ALLSELECT '1678','15','02',0,'2013-8-8'select * from Person[/code]here is SP create statement:[code="sql"] CREATE PROCEDURE [GetPerson] @RecordNumber VARCHAR(16) = NULL , @PersonNo VARCHAR(9) = NULL , @PersonType VARCHAR(2) = NULL , @ActiveOnly BIT = NULL , @ActiveDate datetime = NULLAS BEGIN print 'hello' SET @ActiveDate = ISNULL(@ActiveDate, convert(varchar(10), GETDATE(),101) ) SET @ActiveOnly = ISNULL(@ActiveOnly, 0) IF (@RecordNumber IS NULL AND @PersonNo IS NULL AND @PersonType IS NULL) OR (@PersonNo IS NULL AND @PersonType IS NOT NULL) OR (@PersonNo IS NOT NULL AND @PersonType IS NULL) BEGIN print 'go ifff line' --Get info RETURN END ELSEprint 'go else line'print '@RecordNumber = ' + @RecordNumberprint '@PersonNo = ' +@PersonNoprint '@PersonType = ' +@PersonTypeprint '@ActiveOnly = ' + cast(@ActiveOnly as varchar)print '@ActiveDate = ' + cast(@ActiveDate as varchar) -- this is how i am sing the 3 optinal paramaters to get data from person table IF NOT EXISTS( SELECT 1 FROM Person WHERE (@RecordNumber IS NULL OR RecNo = @RecordNumber) AND (@PersonNo IS NULL OR PerNo = @PersonNo) AND (@PersonType IS NULL OR Pertype = @PersonType) AND Active = @ActiveOnly AND ActiveDate=@ActiveDate ) BEGIN print 'go return' RETURN END END [/code]ANY help on this is Appreciated.....Thanks
↧
Query
Hi all,I am facing a problem in writing a query.Here is my requirementi have a <products> table with columns <productid> <productname> <manufactureDate> <DeliveryDate>and some columns are filled with null valuesi am trying to find the number of null columns with a counter.the execution flow has to be like whenever i come across a null the counter has to be incremented by 1.kindly help me in writing this query.Regards--------------Trainee SQL
↧
Concat columns in Where clause
Hello everyone, I'm searching to see how performance works on columns concat in the where clause. What are the search / performance ramifications? Any suggestions? WHERE[column1] + [column2]
↧
↧
Update values like Vlookup
Hi,I have a table as per below.[code="sql"]CREATE TABLE #TEMP(ID NVARCHAR(200) NOT NULL,SIMNO NVARCHAR(200) NULL,IMEI NVARCHAR(200) NULL)iNSERT INTO #TEMP VALUES ('0412345678','0412345678','013275009174916') iNSERT INTO #TEMP VALUES ('013275009174916','0412345678','') iNSERT INTO #TEMP VALUES ('013568258264650','0412345678','')SELECT * FROM #TEMP -- should look ilke ID SIMNO IMEI013275009174916 0412345678 013568258264650 0412345678 0412345678 0412345678 013275009174916[/code]I want to update values in IMEI column if SIMNo matches.I think it's self join but couldn't come up with any solution...
↧
Calculate Differences and move differences into Next Month
Hi Guys,I need some help here. I have posted the DDL statements below.In my report I have 2 scenario's overlap each other based on when we upload information. But in the database they are 2 separate line items. I need to find the difference between 2 same intersections in different scenario's and move the variance ahead into the next monthIf there is only a unique intersection then just pass that if scenario is Jul.Lets work on moving Nov variances into Dec. Let me know if you have questions. Thanks.Database : [code="other"]fund scenario Year account function dept Oct Nov Dec Jan10 Actual 2014 65000 90 420 200 100 0 010 July 2014 65000 90 422 150 200 444 555[/code][code="other"]Report Structurefund scenario Year account function dept Oct Nov Dec Jan10 July 2014 65000 90 420 200 100 444 555DDL Statement[code="other"]Expected Results :fund scenario Year account function dept Oct Nov Dec Jan10 July 2014 65000 90 420 200 100 544(=200-100+444) 555[/code]Report : [code="sql"]CREATE TABLE [dbo].[test1]( [fund_name] [varchar](80) NULL, [scenario_name] [varchar](80) NULL, [fiscal_name] [varchar](80) NULL, [project_name] [varchar](80) NULL, [account_name] [varchar](80) NULL, [function_name] [varchar](80) NULL, [department_name] [varchar](80) NULL, [planning_year_name] [varchar](80) NULL, [special_name] [varchar](80) NULL, [Jun] [float] NOT NULL, [Jul] [float] NOT NULL, [Aug] [float] NOT NULL, [Sep] [float] NOT NULL, [Oct] [float] NOT NULL, [Nov] [float] NOT NULL, [Dec] [float] NOT NULL, [Jan] [float] NOT NULL, [Feb] [float] NOT NULL, [Mar] [float] NOT NULL, [Apr] [float] NOT NULL, [May] [float] NOT NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOINSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Actual', N'2014', N'0000', N'65000', N'90', N'420', N'None', N'0', 0, 0, 0, 0, 149.92, 0, 0, 0, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Actual', N'2014', N'0000', N'65000', N'90', N'421', N'None', N'0', 0, 85.49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Actual', N'2014', N'0000', N'65000', N'90', N'422', N'None', N'0', 0, 0, 249.28, 15, 10.64, 100.12, 0, 0, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Jul (1+11) Forecast', N'2014', N'0000', N'65000', N'90', N'420', N'None', N'0', 0, 100, 0, 100, 100, 16.67, 33.33, 50, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Jul (1+11) Forecast', N'2014', N'0000', N'65000', N'90', N'422', N'None', N'0', 100, 0, 0, 0, 0, 250, 250, 200, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Jul (1+11) Forecast', N'2014', N'0200', N'65000', N'90', N'422', N'None', N'0', 0, 0, 0, 250, 0, 250, 0, 0, 0, 0, 0, 0)INSERT [dbo].[test1] ([fund_name], [scenario_name], [fiscal_name], [project_name], [account_name], [function_name], [department_name], [planning_year_name], [special_name], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec], [Jan], [Feb], [Mar], [Apr], [May]) VALUES (N'10', N'Jul (1+11) Forecast', N'2014', N'0210', N'65000', N'90', N'422', N'None', N'0', 0, 0, 0, 0, 0, 250, 0, 0, 0, 0, 0, 0)[/code]
↧
Ranking value based on row value
Hi,I would like to create a ranking value based on the row value.For example consider the following sample Declare @t table(val varchar(20))insert @tselect 'test_1' union allselect 'test_2' union allselect 'Total' union allselect 'test_1' union allselect 'test_2' union allselect 'test_3' union allselect 'Total' union allselect 'test_1' union allselect 'test_2' union allselect 'test_3' union allselect 'test_4' union allselect 'Total' union allselect 'test_1' union allselect 'test_2' union allselect 'Total' So what I would like is that all rows before the first instance of 'Total' should be assigned id=1 in a new column,the second instance of 'Total' should be assigned id=2 to all rows before second instance of 'Total' but after the first instance of 'Total'.Id=3 for all rows before third instance of 'Total' but to all rows before the second instance of 'Total'This should be repeated for all the instances of 'Total'.
↧