SQL Server Management Studio - flatten fields list in scripted output - sql-server

In SQL Server Management Studio (2005 and up), when I right-click a table and script out a SELECT or INSERT statement, all columns are listed underneath each other:
SELECT
[field1]
,[field2]
,[field3]
,[field4]
...
FROM dbo.[table1]
For tables with a lot of columns this causes me to lose track of the actual T-SQL flow, so I'd prefer the columns on a single line:
SELECT [field1], [field2], [fields3], [field4], ...
FROM [table1]
Is there any tool (can be an online tool) that takes this output and converts it? Or am I missing an option in the SQL scripting?

You can visit sqlformat, Change option List and Parameters Style to Not Stacked
Or
Even you can use Notepad++ plugin Poor Man T-SQL formatter and Changes it options

You can do it using Find and Replace.
Ctrl+F
Quick Replace
Check Use ->Regular Expressions
Find Wha`: ",\n\t" (comma, line feed and tabulation)
Replace with ", " (comma and space)
Replace All

From Management studio you go to a table or a view, expand it so that you can see the columns folder and then drag the folder onto your query and it will write all the columns of that table in a single row.

Related

Why does SSMS insert a ' TOP (100) PERCENT' into the view that I am trying to write and warn me that ORDER BY may not work?

I am trying to create a SQL View in SSMS. I am using Views because they are easier to invoke from Power BI than Stored Procedures (especially when no parameters are needed).
I start by writing and testing a SQL SELECT query with an ORDER BY clause.
When I copy and paste my query in the New View:
SSMS adds a TOP (100) PERCENT to my SELECT statement.
Tells me that my ORDER BY clause (which works perfectly well in the SQL SELECT) may not work.
If you click the Help button on the dialog, you are taken to a Microsoft "Oops! No F1 help match was found" page.
My questions are:
Is TOP (100) PERCENT not implied when it is left out of a SQL Select?
Why would a View based on a SQL Select statement not like ORDER BY clauses?
SQL views to not support ORDER BY. For more detail on this, see these other posts:
Create a view with ORDER BY clause
Possible to have an OrderBy in a view?
Order BY is not supported in view in sql server
Why use Select Top 100 Percent?
As #Martin Smith said, your options are one of the following:
Put the ORDER BY in a query that references the view.
SELECT * FROM ViewName ORDER BY [...]
Do the ordering in the Power Query Editor. If you don't have any steps before this sort that break query folding, this should be translated into a native SQL query that gets evaluated on the SQL Server.
I recommend the latter since further steps can also potentially be folded in as well. Specifying your own query does not support query folding.

Copy only column header from resultset in microsoft sql server management studio

I want to copy only 'Column Headers' from the result set in Microsoft Sql Server Management Studio but I didn't find any option. Same option is there is Oracle SQL server management. I googled about it but didn't find any thing that would help me so wanted to check if there a way to do this
Run your query without any result-set using
select * from tablename where 1=2
then use "Copy with Header".This is the only way to copy only columns header.
Instead of result into grid please use Result to text. Result to Text - Ctrl+3
Hope this helps.
No straight method as far as I know. One method is to output the result to text and copy only the header. Another trick is to execute the query without returning any results to grid by applying a false predicate like SELECT column_list FROM yourtable WHERE 1 = 2; then, use 'Copy with Headers' option.
Use SET FMTONLY ON before execute query .
It,s used to test the format of the response without actually running the query.
Choose results to grid and choose copy with headers option .
mark the table name and run alt+F1.
than you can copy directly from the cells of the Column_name table
SSMSBoost appears to have this as a feature. It is an addon for SQL Server Management Studio but the Pro edition (which you need for this feature) will cost you US$195.
(I am not affiliated with this product)

ETL Script to dynamically map multiple EXECUTE SQL resultset to multiple tables (table name based on sql file provided)

ETL Script to dynamically map multiple execute sql resultset to multiple tables (table name based on sql file provided)
I have a source folder with sql files ( I can put it up as stored procedures as well ) . I know how to loop and execute sql tasks in a foreach container. Now the part where I'm stuck is I need to use the final result set of each sql queries and shove it into a table with the same name as the sql file.
So, Folder -> script1.sql , script2.sql etc -> ETL -> goes to table script1, table script2 etc.
EDIT : Based on the comment made by Joe, I just want to say that I'm aware of using insert within a script but I need to insert it onto a table in a different server.And Linked servers are not the ideal solutions
Any psuedocode or link to tutorials will be extremely helpful . Thanks!
I would add the table creation to the script. It is probably the simplest way to do this. If your script is Select SomeField From Table1, you could change it to Select SomeField Into Table script1 From Table1. Then there is no need to map in SSIS which is not easy to do from my experience.

Instead of ';' how to indicate end of batch in SQL Server Management Studio (SMSS)?

In Sql Developer (SqlDev) (the Oracle tool) I most of the time use ';' to indicate the end of a batch.
Also we use the ';' when having a larger script with lots of batches in it. For example a script where the first batch creates a table, the second inserts data in that table, the third does a join with the just created table and another table, etc.
On SqlDev the script (with the different batches in it) works fine. But when we copied the exact script to SQL Server Management Studio (SMSS) and ran it, it gave errors that the table (of the third batch where the created table is joined) does not exist.
How can I make the scipt run on SMSS without the script failing?
In SQL server you can use 'GO' to split a batch or block of statement.
something like below.
ALTER TABLE [dbo].[Security] ADD CONSTRAINT [DF_Security_ImportSettings] DEFAULT ((11111011111111111.)) FOR [ImportSettings]
GO
ALTER TABLE [dbo].[Security] ADD CONSTRAINT [DF_Security_PricingType] DEFAULT ((-1)) FOR [PricingType]
GO
ALTER TABLE [dbo].[Security] ADD CONSTRAINT [DF_Security_AutoUpdateCustomPricing] DEFAULT ((1)) FOR [AutoUpdateCustomPricing]
GO
Go is the Keyword you are looking for ..
Example..
insert into t1
select 1
go
alter table t1
add abc int
this is also configurable in SSMS(i haven't tested though) to ; or some other word..
It appears that in SQL Server Management Studio (SMSS) sometimes it is needed to use 'GO' instead of ';'.
The ';' works differently in SQL Server Management Studio (SSMS) compared to the use of ';' in for example SQl Developer (SQLDev) (the Oracle tool).
In SQLDev the ';' acts as a end of batch indicator, where SSMS doesn't see it when using DLL. Instead SMSS first looks at the whole script and thinks of smart ways to run it. Which means all is run parallel, where some batches are dependent of others, but they are not run properply and gives failure of the script.
In my situation it meant I had to use 'GO' to tell the DBMS to run the first, second and third sequantial instead of parallel. I changed all the ';' with GO in the script (in fact it has a whole lot more batches in it) and that did the trick. I'm not sure it is completely right to do it this way, but at least it worked. :)
Also see:
What is the use of GO in SQL Server Management Studio & Transact SQL?
When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

SQL Server 2005: How to automatically include database name and brackets in queries

Good day,
In SQL Server 2005, when I write a new query, I like to drap and drop table and column names from the object explorer. However, when I drag and drop a table, is there a way to automatically include the database name?
(example: when I drag and drop the table Table1 in the query designer, I would like to have Database1.dbo.Table1 instead of just Table1)
Also, is there a way to automatically include '[' and ']' around the column and table names?
Thank you!
Sorry, not from drag and drop from object explorer.
One of the tools suggested in this question may help:
Need a tool to automatically indent and format SQL Server stored procedures

Resources