Editing SQL query with Visual Studio 2008 - sql

Would you recommend me the best approach to edit SQL query with Visual Studio 2008 Professional, please?
I know I can open Query window from context menu in Server Explorer and edit text in SQL Pane. But unfortunately I am not allowed to save query to a file and Find and Replace commands are not working there.
Alternatively I can open text file with extension .SQL in Visual Studio editor obtaining syntax highlighting and full editing capabilities but losing possibility to execute the script.
Now I copy query text from one window to another and back but I hope there is better solution.
Many thanks for suggestions!

When I have to connect to Sql Server, I use NetBeans, as it has a number of features that make it much easier to use than Visual Studio. One of these is intellisense, which is very useful when you have an alias for a table name and lots of long field names. The general handling of connections and connection pools is also much cleaner in NetBeans. I could go on, but suggest trying it for yourself. You will almost certainly need the Sql Server jdbc driver, which can be downloaded from http://msdn.microsoft.com/en-us/data/aa937724.aspx

If you create a Database project within your solution in Visual Studio, then you can set up a default database connection for that project. Then any *.sql files that are included in the database project can be executed against that connection. What I usually do is select the text to be exectued and right-click it, then select "Run Selection".
You can set up any number of database connections under the "Database References" node in the solution explorer, and choose the one you want to run your query against.

Wow you are right, this is a real pain. Would it be acceptable for you to use a local copy of the SQL Management Studio (the name might be wrong)? You would be outside of VS but you could still edit your query and run it at the same time. Definitely not optimal but better than nothing.
Good luck.

Related

How to change the connection in Sql Server Data Tools Editor in Visual Studio

My goal is to keep SQL Server stored procedures under source control. I also want to stop using SQL Server Management Studio and use only Visual Studio for SQL related development.
I've added a new SQL Server Database project to my solution. I have successfully imported my database schema into the new project, and all the SQL objects (tables, stored procedures) are there in their own files.
I know that now if I run (with F5) the .sql files then my changes will be applied to my (LocalDB). This if fine, but what if I want to very quickly run something on another machine (like a dedicated SQL Server shared by the entire team)? How can I change the connection string of the current .sql file in the Sql Server Data Tools editor?
I have the latest version of Sql Server Data Tools extension for Visual Studio 2012 (SQL Server Data Tools 11.1.31203.1). I don't know if this is related to the current version, but I cannot find anymore the Transact-SQL Editor Toolbar.
I have also tried to Right-click on the sql editor, choose Connection -> Disconnect. If I do the reverse (Connection -> Connect...) the editor directly connects automatically (probably to my LocalDB), without asking me a dialog to choose my connection.
Another strange thing I've observed, if I try to run a simple SQL query (like select * from dbo.ApplicationUser I receive the following message (even if the autocomplete works):
Thanks.
(Note: I have the same issue with Visual Studio 2013)
Inspired by srutzky's comments, I installed the latest SSDT pack (12.0.41025). And bingo, like srutzky said there is a Change Connection option. But what's more, you can specify your Target DB by right clicking on the Project in the Solution Explorer, and going to Properties->Debug and changing the Target Connection String! If you're stuck on an older SSDT, then the below instructions will still work.
For SSDT 12.0.3-
I've also been plagued by this problem! My solution is below, but it has some Pros and Cons to it...
SOLUTION
I'm assuming that you are using a SQL Server Project in VS (I'm using VS2013 and SQL Server 2012).
Right click on your .sql file in the Solution Explorer and view Properties.
Change Build Action to None.
If the file is open for editing, then close it.
Reopen the file, and the T-SQL Editor should appear at the top.
Click Connect, and you will connect to your (localdb).
Click Disconnect.
Click Connect again and the SQL Server Connection dialog should appear.
Switch the connect string of '(localdb)\Whatever' to '.' (for some reason using '(localhost)' didn't work for me).
Voila, you should now be able to query against your SQL Server DBs! Rinse and repeat for every file you want this capability with... :/
PROS
You can finally run queries directly against your SQL Server DB
Your code can be organized in a nice VS solution (SSMS doesn't allow folders! :/)
You can (after switching Build Action setting back) Build the project
CONS
I'm not seeing any autocomplete/intellisense against the remote DB, although if you import your DB, then you could gain the intellisense from that
Requires each file to switch Build Action to None
This should be a fairly simple and straight-forward thing to do, that is, if you are using SSDT version 12.0.41025.0 (or newer, one would suppose):
Do either:
Go to the SQL menu at the top of the Visual Studio window
Right-click inside of the SQL editor tab
Go to Connection ->
Select Change Connection
Then it will display the "Connect to Server" modal dialog window.
If you do not see the options for "Disconnect All Queries" and "Change Connection...", then you need to upgrade your SSDT via either:
Visual Studio:
Go to the "TOOLS" menu and then "Extensions and Updates..."
Direct download:
Go to: http://msdn.microsoft.com/en-us/data/tools.aspx
The fastest way to achieve this is create a new SQL Connection, copy and paste the code then execute.
What I do is Tools->SQL Server->New Query.
Enter the database credentials (And make sure that the Database at the top is correct - I have hundreds of sp's in my master db on local :) )
Copy the source code from the editor, paste into the new query window.
Then Execute (CRTL-Shift-E).
You can leave this 'scratch' window open and pinned for easy access for subsequent executes.
If you want to deploy (i.e. publish) the entire database then you can setup a publish destination for each server, right click on the xml and select publish..

How can I get SSMS to provide Auto Completion for SQL Azure?

I'm trying to identify a SQL Server Management Studio option (when writing/running queries) that provides table/column auto-complete functionality in the query editor. Unfortunately, SSMS seems to stop giving you Intellisense when you're connected to a SQL Azure database. Is there any way to fix this?
Are there any options, hacks, plugins or anything else that can accomplish this?
I've finally stumbled upon an option. It's non-ideal but it is certainly a huge step in the right direction!
dbForge SQL Complete is a SSMS plugin that replaces SSMS's built-in Intellisense with its own auto-complete engine. This is a HUGE improvement when connected to SQL Azure, but so far the free version feels like a step backwards when connected to traditional SQL Server instances. Overall, I think we're going to prefer using this over not using it. I'll come back in a couple days to report how well (or not) it's going.
At least it's an option, though!
New Release of SQL Server Management Studio V17.2 now support Intelliscence so no need to have any other tools for this.
Please note that this will only work in case of SQL Server Authentication, that means logged in using SA Instance.
https://connect.microsoft.com/SQLServer/feedback/details/3100677/ssms-2016-would-be-nice-to-have-intellisense-on-azure-sql-databases
Visual Studio have enabled basic IntelliSense for Azure SQL, but it isn't avalaible easily.
You have to click on table, select from drop menu DROP AND CREATE TO -> New query window and then IntelliSense will work. If you simply use New query it will not.

SQL table view (Visual Studio or...)

Is there any way to read .SQL document in table view in VisualStudio or any other program?
.sql quite simply contains a set of executable SQL instructions. ( described here ) The instructions are in plain-text and are can be viewed with any text editor (i.e. notepad).
Based on the way your question is phrased, I assume you have instructions to create a table in SQL which you would like to view in a more user-friendly way. The best way I can think of to do this (although I am not extensively familiar with Visual Studio), is to set up a test sql environment (on localhost/a testing server - XAMPP comes to mind). Then open up phpmyadmin (or another database manager, although phpmyadmin is packaged with xampp) and run your code... and, if teh code is not your own, obviously look for sql injections ;) .

How to get script of SQL Server data? [duplicate]

This question already has answers here:
What is the best way to auto-generate INSERT statements for a SQL Server table?
(24 answers)
Closed 6 years ago.
I'm looking for a way to do something analogous to the MySQL dump from SQL Server. I need to be able to pick the tables and export the schema and the data (or I can export the schema via SQL Server Management Studio and export the data separately somehow).
I need this data to be able to turn around and go back into SQL Server so it needs to maintain GUIDs/uniqueidentifiers and other column types.
Does anyone know of a good tool for this?
From the SQL Server Management Studio you can right click on your database and select:
Tasks -> Generate Scripts
Then simply proceed through the wizard. Make sure to set 'Script Data' to TRUE when prompted to choose the script options.
SQL Server 2008 R2
Further reading:
Robert Burke: SQL Server 2005 - Scripting your Database
SQL Server Management Studio
This is your best tool for performing this task. You can generate a script that will build whichever tables you wish from a database as well as insert the data in those tables (as far as I know you have to export all of the data in the selected tables however).
To do this follow these steps:
Right-click on your database and select Tasks > Generate Scripts
In the Generate and Publish Scripts wizard, select the "Select specific database objects" option
Expand the "Tables" tree and select all of the tables you wish to export the scheme and data for, then click Next
In the next screen choose how you wish to save the script (the Output Type must remain set as "Save scripts to a specific location"), then click the Advanced button in the top right corner
In the newly opened window, under the General section is a setting called "Types of data to script", set this to "Scheme and data" and click OK
Click Next, review the export summary and click Next again. This will generate the script to your selected destination.
To restore your database, simply create a new database and change the first line of your generated script to USE [Your.New.Database.Name], then execute. Your new database will now have all of the tables and data you selected from the original database.
I had a hell of a time finding this option in SQL Management Studio 2012, but I finally found it. The option is hiding in the Advanced button in the screen below.
I always assumed this contained just assumed advanced options for File generation, since that's what it's next to, but it turns out someone at MS is just really bad at UI design in this case. HTH somebody who comes to this thread like I did.
If you want to script all table rows then
Go with Generate Scripts as described by Daniel Vassallo. You can’t go wrong here
Else
Use third party tools such as ApexSQL Script or SSMS Toolpack for more advanced scripting that includes some preprocessing, selective scripting and more.
Check out SSMS Tool Pack. It works in Management Studio 2005 and 2008. There is an option to generate insert statements which I've found helpful moving small amounts of data from one system to another.
With this option you will have to script out the DDL separately.
SqlPubWiz.exe (for me, it's in C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Publishing\1.2>)
Run it with no arguments for a wizard. Give it arguments to run on commandline.
SqlPubWiz.exe script -C "<ConnectionString>" <OutputFile>
BCP can dump your data to a file and in SQL Server Management Studio, right click on the table, and select "script table as" then "create to", then "file..." and it will produce a complete table script.
BCP info
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=319
http://msdn.microsoft.com/en-us/library/aa174646%28SQL.80%29.aspx
I know this has been answered already, but I am here to offer a word of warning.
We recently received a database from a client that has a cyclical foreign key reference. The SQL Server script generator refuses to generate the data for databases with cyclical references.

How do I edit a text field (or ntext) in SQL Server 2000 or 2005 using the GUI?

Is there a way to do this with SQL Server tools?
If not, is there a 3rd party tool that does this?
There doesn't seem to be a good way to edit longer text columns in the SQL Server Managers for SQL Server 2000 or 2005. While SQL Server Manager is really not for editing data in your db, what other tool does Microsoft provide that would normally allow you to do this? Every other field is pretty easy to edit, except long text fields. In Access, you could hit shift-f2 and it would pop up a nice dialog to edit your text in.
alt text http://techinterview.org/edit.png
Generally, SQL Management Studio is an administrative tool for your database and not meant for data entry other than a quick edit here or there. Generally you would script the data or it would be entered by an application that uses the database for persistence.
(Although I have pointed Access to my SQL Server DB for a better quick and dirty UI.)
This may fight your bill - SQL LOB Editor.
The other option you might want to look at is EMS SQL Studio for SQL Server.
Marc
If you are specifically after a nice big multi-line edit dialogs, then yes you definitely need to look outside of the Microsoft SSMS line of tools. They don't support it.
This is just stupid. Enterprise Manager for SQL Server 2000 handled multiline text just fine. Just silly to go backward and loose functionality that was pre-existing. Not like it is rocket science to reproduce formatted text. Someone really dropped the ball on this one.
In the Management Console, isn't it possible to do an "Open Table" context-menu action and then edit the data from there?
I totally recommend DBVisualizer. The nice thing about it is, it supports a long list of databases and generic all JDBC Drivers (since it is written in Java). You can browse your various databases, change data and explore schema in nice graphs. It comes as a free and a personal edition for 149 $ and is totally worth it! Look at this matrix for a comparison.
You'll be able to edit text and ntext in SQL Server 2000, 2005 and surly for upcoming versions as well.
If I had to make the edits only occasionally I would probably use SQL Query Analyzer and just script the UPDATE command.
If that was too inconvenient I would next look at linking to the database in Access, and to really quick and dirty ease of use I would just use an AutoForm to generate a UI for the table. If you don't have Access I believe OpenOffice Base can connect through ODBC and has similar form building functionality.