December 22, 2020 at 2:36 pm
Hi All,
I have a need to replace, in various views, the SELECT statement that produces the results (SELECT --> SELECT TOP 10). These queries could be in any of the following formats:
--1) single select
SELECT *
FROM test
--2) CTE with single select
WITH test
AS (SELECT 1 a),
test2
AS (SELECT 2 b)
SELECT *
FROM test
--3) Multiple selects with union
SELECT *
FROM test
UNION ALL
SELECT *
FROM test2
--4) Multiple selects with union all and with outer select
SELECT *
FROM (
SELECT *
FROM test
UNION ALL
SELECT *
FROM test2
) t
--5) CTE with multiple selects (with union all)
WITH test
AS (SELECT 1 a),
test2
AS (SELECT 2 b)
SELECT *
FROM test
UNION ALL
SELECT *
FROM test2
In cases, like #s 1 and 4, I can try to find the first occurrence of a SELECT (that's not a comment). In some other cases, like #2, I can try to find the last occurrence of a SELECT (that's not a comment). However, in cases like #s 3 and 5, I'm having to contend with UNION ALLs, and would want ALL of the SELECTs involved in the UNION ALL to be replaced with SELECT TOP 10.
I realize this is a little complicated, but I don't think it's impossible. I'm trying to talk it through in English to see how I might approach this to accommodate all of these cases, but I could use some help if anyone has any suggestions.
Thanks in advance!
Mike
Mike Scalise, PMP
https://www.michaelscalise.com
December 22, 2020 at 2:52 pm
I presume you've got a lot of this to do & you want to build an automated way of doing it.
The way I've approached tasks like this in the past is something like this:
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 22, 2020 at 3:24 pm
I presume you've got a lot of this to do & you want to build an automated way of doing it.
Correct. I have quite a few.
I would do this in C#, but any sort of script which can modify the .sql files and search using Regular Expressions should be sufficient.
I am actually looking to do this in T-SQL, if possible. I know, it would be much easier with a robust language like C# that support regex, but I am trying to keep it all in T-SQL.
I'm just curious--given these restrictions, any other thoughts?
Mike
Mike Scalise, PMP
https://www.michaelscalise.com
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply