Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an extra row for each row in the result set in SQL?

Tags:

sql

sql-server

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:

Enter image description here

I need an extra row with text 'GO' after every record like this:

Enter image description here

Is there a way to do this? I tried with a foreach statement, but I was unable to replicate the desired results.

like image 638
Anup Avatar asked Dec 27 '16 11:12

Anup


People also ask

How do I select multiple rows in one row in SQL?

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.

How do I select specific rows in SQL?

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.

How do I select a certain number of rows?

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.


1 Answers

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') 
like image 138
Shakeer Mirza Avatar answered Oct 01 '22 06:10

Shakeer Mirza