Robert Cary

  • Interests: Sleight of hand magic, cycling, bowling, poker

SQLServerCentral Article

Searching Syscomments Accurately

As a SQL Server DBA you should know that your code is stored in syscomments by default. While most DBAs use version control systems, there are times you might want to look through the code on the server for comparison purposes. Robert Cary brings us an article on how you can do this in 2000 and 2005.

4.8 (5)

You rated this post out of 5. Change rating

2008-01-07 (first published: )

8,390 reads

Technical Article

Find Nth Occurrence of Character Function (Set Based)

This function was originally contributed by other visitors. Below is an example of a set based approach to the problem. This script requires a numbers table (see SqlServerCentral article http://www.sqlservercentral.com/columnists/mcoles/2547.asp for more details)Please note that this code is uses SQL2005 featuresEnjoy!Robert Caryhttp://tsqlland.blogspot.com

5 (1)

You rated this post out of 5. Change rating

2006-10-20 (first published: )

546 reads

Blogs

FREE Window Functions Course (This Month Only!)

By

Want to really level up your SQL game? I mean, go from good to great? This March...

Tally Table Alternatives: #SQLNewBlogger

By

We published an article recently at SQL Server Central on Tally Tables in Fabric...

Real-Time Intelligence in Microsoft Fabric

By

In today’s data-driven world, organizations need the ability to analyze and act on data...

Read the latest Blogs

Forums

Dynamic T-SQL Script Parameterization Using Python

By omu

Comments posted to this topic are about the item Dynamic T-SQL Script Parameterization Using...

Solutions for Environmental Testing | Fare Labs

By farelabs

The state-of-the-art FARE Labs Environmental Testing Laboratory provides a comprehensive variety of testing services...

Database restoration slowed down abnormally.

By JasonO

I'm running some restoration activity as part of a rehearsal to prepare for an...

Visit the forum

Question of the Day

Inserting Sequences

I created a new sequence in SQL Server 2022 with this code.

CREATE SEQUENCE myseqtest START WITH 1 INCREMENT BY 1;
GO
I want to use this to insert some data from another table into a new table with this sequence. Which of these queries shows the way to do this efficiently?
-- 1
INSERT dbo.NewMonthSales
  (SaleID, saleyear, salemonth, currSales)
SELECT
  NEXT VALUE FOR myseqtest
, ms.saleyear
, ms.salemonth
, ms.currMonthSales
FROM dbo.MonthSales AS ms;
GO

-- 2
INSERT dbo.NewMonthSales
  (SaleID, saleyear, salemonth, currSales)
SELECT
  NEXT VALUE 
, ms.saleyear
, ms.salemonth
, ms.currMonthSales
FROM dbo.MonthSales AS ms, myseqtest;
GO

--3 
DECLARE mycurs CURSOR FOR SELECT
 ms.saleyear
, ms.salemonth
, ms.currMonthSales
FROM dbo.MonthSales AS ms

DECLARE @yr INT, @mn INT, @sales NUMERIC(10,2)

FETCH NEXT FROM mycurs INTO @yr, @mn, @sales
WHILE @@FETCH_STATUS = 0
 BEGIN
   INSERT dbo.NewMonthSales
    (SaleID, saleyear, salemonth, currSales)
    SELECT
      NEXT VALUE FOR myseqtest
    , @yr
    , @mn
    , @sales
   FETCH NEXT FROM mycurs INTO @yr, @mn, @sales
 END

See possible answers