I have the below query:
SELECT '['+name+']' FROM sys.schemas
Where name <> 'dbo' AND name <> 'guest' AND name <> 'INFORMATION_SCHEMA'
....
The result of this query is simple:
I need an extra row with text 'GO' after every record like this:
Is there a way to do this? I tried with a foreach
statement, but I was unable to replicate the desired results.
Concatenate Rows Using COALESCE This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.
To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.
Select one or more rows and columns Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.
Just add Go statement to the Dynamic code by hitting ENTER key and see the result pasting in SSMS. This will populate with GO
statement in Next line.
You should also use QUOTENAME rather than concatenating the square brackets yourself (to work correctly for names containing ]
) and the where clause can be simplified with NOT IN
.
SELECT QUOTENAME(name) +'
GO
'
FROM sys.schemas
WHERE name NOT IN ('dbo', 'guest', 'INFORMATION_SCHEMA')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With