SQL Server 2012 \ VS 2010 - SQL syntax highlighting for custom file extensions - sql

At work we have a variety of file extensions for our SQL code: .PRC for store procedures, .TAB for table scripts etc etc.
In previous versions of SQL Server Management Studio I could set each extension to use SQL syntax highlighting in the Tools > Options > Text Editor > File Extension menu.
But in SQL Server 2012 the option for "SQL Query Editor" is gone:
Anyone know of a work around, I've tried some registry hacks to no avail)

I added a * as a file extension that is handled by the SQL Query Editor and now I have the "SQL Query Editor" option in the drop down list of available editors. The reg key I edited was:
HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management
Studio\11.0_Config\Editors{B5A506EB-11BE-4782-9A18-21265C2CA0B4}\Extensions
I matched the DWORD value to that of the "sql" extension.
EDIT:
It looks like this only worked for the session life of SMSS. When I closed and reopened SMSS, the PRC mapping was erased and the DWORD key that I created was removed. Hopefully I can find a way to keep it persistant.

Related

How to set column's +comment+ with CREATE TABLE and SELECT statement in sql [duplicate]

In the SSMS (currently running SQL Server 2008 R2), I would like to add the Description property next to the Allow Nulls property in the table designer window.
I have looked at the registry change for setting the default value of the null property as shown in this SO answer How do I set the Allow Nulls property in table designer of SSMS to be always false? However, I cannot figure out how to add the new property into the new table designer view.
I thought the the following registry entry would help.
HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\100\Tools\Shell\DataProject\
SSVPropViewColumnsSQL70
SSVPropViewColumnsSQL80
I changed the registry entries of above keys from 1,2,6; to 1,2,6,9; but nothing changed in the designer.
Does anyone have any additional thoughts on this?
Here are the steps to add a property to the table designer in SQL Server Management Studio. The steps involve altering the values in registry settings.
NOTE: Please be careful while altering registry keys.
Type regedit in the Windows Start --> Run command to open the Registry Editor.
Navigate to HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\100\Tools\Shell\DataProject
You might need to change the SQL Server version accordingly. I am using SQL Server 2008 R2 Express and hence the version 100. For SQL Server 2012, I found this setting under HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\11.0\DataProject
Under the above mentioned registry path, look for the keys SSVPropViewColumnsSQL70 and SSVPropViewColumnsSQL80.
By default, these registry keys will have the values 1,2,6;. Section Property sequence mentioned below shows the number associated with each property. For my requirement to add Description column to the table designer, I had to change the registry key values to 1,2,6,17;
Right-click on the key and select Modify option. Change the value from 1,2,6; to 1,2,6,17;. This has to be done on both the keys SSVPropViewColumnsSQL70 and SSVPropViewColumnsSQL80
NOTE: Remember to restart SSMS between each registry change.
Property sequence:
Column Name
Data Type
Length
Precision
Scale
Allow Nulls
Default Value
Identity
Identity Seed
Identity Increment
Row GUID
Nullable
Condensed Type
Not for Replication
Formula
Collation
Description
Hope this helps someone.
For those of you looking for a .REG file to achieve this, copy/paste these lines into a text file with a .REG extension. Double click that to add it to your registry. The column numbers are listed in the answer by pithhelmet. The example below uses "14.0" meaning the version of SSMS that started being released as a standalone tool from SQL 2017 onwards. I expect that the registry path will continue to stay at 14.0 for some time yet, even with regular updates for SSMS being released.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\14.0\DataProject]
"SSVPropViewColumnsSQL70"="1,2,6,3,7,8,17;"
"SSVPropViewColumnsSQL80"="1,2,6,3,7,8,17;"
Note that you need to have SSMS closed when you apply these changes to the registry.
For SQL Server Management Studio Version 18.x (2019):
Open Regedit and go to: "Computer\HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\18.0_IsoShell\DataProject" and then Change the SSVPropViewColumnsSQL70 and SSVPropViewColumnsSQL80 Data from 1,2,6; to 1,2,6,17
For SQL-Server-Management Studio 2014 (SSMS 2014) it's a bit tricky to get the "Description" column (17):
1)
Open SSMS 2014, wait for Login-Dialog is shown. (Don't click "connect"!)
2a)
Open Regedit, goto:
"HKEY_USERS\S-1...###YOUR-WINDOWS-USER-SID###-\SOFTWARE\Microsoft\SQL Server Management Studio\12.0\DataProject"
2b)
Change the keys: SSVPropViewColumnsSQL70 and SSVPropViewColumnsSQL80 from
1,2,6; to 1,2,6,17;
3)
Now click "Connect" at the SSMS 2014 Login-Dialog.

import a DBF file in SQL Server using SQL Script

How can I import a .dbf file into SQL Server using a SQL script?
Found answers from this post, but unfortunately none of them work to me :( :
Trying to Import FoxPro DBF File to SQL Server
and
How to import a DBF file in SQL Server
When I'm trying this code :
SELECT *
INTO [APP_DB]..[BILLHEAD]
FROM OPENROWSET('MSDASQL', 'Driver=Microsoft Visual FoxPro Driver; SourceDB=D:\DBF; SourceType=DBF', 'SELECT * FROM BILLHEAD')
I get this error:
OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". Msg 7303, Level 16, State 1, Line 1 Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)".
And also, when trying this :
SELECT *
FROM openrowset('VFPOLEDB','D:\DBF\BILLHEAD.dbf';'';
'','SELECT * FROM BILLHEAD')
I get this error :
Msg 7438, Level 16, State 1, Line 1
The 32-bit OLE DB provider "VFPOLEDB" cannot be loaded in-process on a 64-bit SQL Server.
I don't want to download any third party application. That's why I'm trying all the possible solution and I need your help now guys. I'm creating a small application to import .DBF files into SQL Server.
Regards,
You are using 64-bit SQL sever, but FoxPro OLE DB driver is 32-bit. You need to consult this article which discusses how to use the 32-bit OLE DB driver with 64-bit SQL Server.
Gimo, I'm not sure this will work and I'm no MS SQL Server expert, but I've been wrestling with a similar problem lately and I have an idea. I think you may be able to get that first block of code from your question to work if you execute the following statements first:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE;
GO
This may not work if you don't have adequate permissions (which happened in my situation), but it may be worth a shot.
Our office SQL/GIS guru, Burce, solved a similar problem I was having. I'm not sure of all the details of how he did it, so while I am reluctant to enter this as an "Answer" (it is too many characters to enter as a Comment) I'll describe what I can in case it is helpful for anyone. First be aware that he has full permissions on the SQL Server, so this solution may not be feasible for all DB users to implement. Bruce set up a Linked Server that's connected to a directory ".../DBF/" on our LAN file server. He also set up a similar Linked Server & directory for CSV files. Anyone in our office can simply copy a DBF file to this directory and then access it in SQL Server as if it were a regular table in a SQL Server database. I access this in SSMS by connecting to the Database Engine then going to Server Objects > Linked Servers > "DBF" > Catalogs > default > Tables > file name . The Properties of the Linked Server say the following:
From General tab of Properties window
From Security tab of Properties window
From Server Options tab of Properties window
Note that this may or may not be a secure configuration for all database server environments, but this is on a SQL Server that is on our internal network, only accessible within our office, with no endpoints or access outside our LAN (it's not used as a server for web, or other internet services).
I have had similar problems where stuff just wasn't working trying to move legacy tables from VFP to SQL 2008R2 and used the following procedure:
select table within VFP
copy to blahblah xl5
Step 2 results in an excel file
Use SQL 2008 R2 or higher "Import and Export Data (32 bit)" to import the excel file.
I was running Windows 7 64 bit and still had to use the 32 bit import to make it work smoothly.
This may explain why you need the 32 bit Import: https://msdn.microsoft.com/en-us/library/ms141209.aspx

Exporting a table from SQL Server 2008 R2 to a file WITHOUT external tools

I would like to export a table from SQL Server 2008 R2 to a file. The problem is that I don't have bcp (nor can I install it or anything else) and am not able to run xpcmdshell. Anyone have any ideas on how this could be done without those permissions/tools? (I would like to have this happen on some automated basis preferably)
I'm usually using Copy/Paste from SSMS Results Pane to Excel
OR
you can right click on database in the Object Explorer and select Database->Tasks->Export Data. An SQL Server Import and Export Wizard dialog opens and you will be able to export data from any table or query to the file or another destination.
OR
you can use LinqPad - awesome, simlpe and free tool (I really love it) that doesn't require installation
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You can also use a command like this too:
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product')
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial: http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
If you have SQL Server 2012 you could add File Tables to your database. Thus you could use SQL Agent to schedule a simple stored proc to update the file table when desired.
http://technet.microsoft.com/en-us/library/ff929144.aspx#Description
it has something called "Query Analyzer"
Query Analyzer (isqlw.exe) is the SQL 2000, pre-SSMS, query tool. A very fine tool. Among other things, is capable of exporting query results to a file. See https://stackoverflow.com/a/3769766/105929:
go to the Tools -> Options menu. On the Results tab, choose to send your output to a CSV file and select the "Print column headers" option.

How to remove "Server name" items from history of SQL Server Management Studio

When trying to connect to a server in Management Studio (specifically 2008), there is a field where you enter the Server name. That field also has a drop-down list where it shows a history of servers that you have attempted to connect to.
How to remove an individual item
from that history?
How to remove an
item from the Login field history
for each Server name?
As of SQL Server 2012 you no longer have to go through the hassle of deleting the bin file (which causes other side effects). You should be able to press the delete key within the MRU list of the Server Name dropdown in the Connect to Server dialog. This is documented in this Connect item and this blog post.
Note that if you have multiple entries for a single server name (e.g. one with Windows and one with SQL Auth), you won't be able to tell which one you're deleting.
Here is simpliest way to clear items from this list.
Open the Microsoft SQL Server Management Studio (SSMS) version you want to affect.
Open the Connect to Server dialog (File->Connect Object Explorer, Object Explorer-> Connect-> Database Engine, etc).
Click on the Server Name field drop down list’s down arrow.
Hover over the items you want to remove.
Press the delete (DEL) key on your keyboard.
there we go.
For SQL 2005, delete the file:
C:\Documents and Settings\<USER>\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat
For SQL 2008, the file location, format and name changed:
C:\Documents and Settings\<USER>\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin
How to clear the list:
Shut down all instances of SSMS
Delete/Rename the file
Open SSMS
This request is registered on Microsoft Connect
Over on this duplicate question #arcticdev posted some code that will get rid of individual entries (as opposed to all entries being delete the bin file).
I have wrapped it in a very ugly UI and put it here: http://ssmsmru.codeplex.com/
For SQL Server 2012 Management Studio, this file has moved. It is now located at:
C:\Users\<username>\AppData\Roaming\Microsoft\
SQL Server Management Studio\11.0\SqlStudio.bin
In Windows Server 2008 standard with SQL Express 2008, the "SqlStudio.bin" file lives here:
%UserProfile%\Microsoft\Microsoft SQL Server\100\Tools\Shell\
Here is an easy way.
Open the connection window, click on the Server name dropdown, and hover over the connection string you want to delete, then press delete.
Delete the file from above path: (Before delete please close SSMS)
File location path for the users of SQL Server 2005,
C:\Documents and Settings\%USERNAME%\Application Data\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat
File location path for the users of SQL Server 2008,
Note: Format Name has been changed.
C:\Documents and Settings\%USERNAME%\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin
File location path for the users of Server 2008 standard/SQL Express 2008
C:\Documents and Settings\%USERNAME%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin
File location path for the users of SQL Server 2012,
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\SQL Server Management Studio\11.0\SqlStudio.bin
File location path for the users of SQL Server 2014,
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin
Note: In SSMS 2012 (Version 10.50.1600.1 OR Above), ow you can remove the server name by selecting it from dropdown and press DELETE.
In SSMS 2012 there is a documented way to delete the server name from the "Connect to Server" dialog. Now, we can remove the server name by selecting it in the dialog and pressing DELETE.
File SqlStudio.bin actually contains binary serialized data of type "Microsoft.SqlServer.Management.UserSettings.SqlStudio".
Using BinaryFormatter class you can write simple .NET application in order to edit file content.
From the Command Prompt (Start \ All Programs \ Accessories \ Command Prompt):
DEL /S SqlStudio.bin
This is the correct way of doing it
http://blogs.msdn.com/b/managingsql/archive/2011/07/13/deleting-old-server-names-from-quot-connect-to-server-quot-dialog-in-ssms.aspx
For Windows Vista and SQL Server 2005,
Delete this file, or open it with the Notepad and clear the server names that you want Clear from the history
%UserProfile%\Microsoft\Microsoft SQL Server\90\Tools\Shell\mru.dat
C:\Users\\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell
Rather than deleting or renaming this file:
Close SQL Server Management Studio.
Find the appropriate file (see the other posts).
Open the .bin in a text/hex editior like NotePad++.
Search for the name of one of the servers and identify the line number.
Make a copy of the .bin/.dat file.
Delete that line. Make sure you delete the entire line, it's possible if you have many the line could wrap.
Open SQL Server Management Studio. Your dropdown will be blank.

SQL Server 2005 - Export table programmatically (run a .sql file to rebuild it)

I have a database with a table Customers that have some data
I have another database in the office that everything is the same, but my table Customers is empty
How can I create a sql file in SQL Server 2005 (T-SQL) that takes everything on the table Customers from the first database, creates a, let's say, buildcustomers.sql, I zip that file, copy it across the network, execute it in my SQL Server and voila! my table Customers is full
How can I do the same for a whole database?
This functionality is already built in to Sql Server Management Studio 2008.
Just download the trial and only install the client tools (which shouldn't expire). Use Management Studio 2008 to connect to your 2005 database (its backwards compatible).
Right click your database
Choose Tasks > Generate Scripts
Press Next, select your database again
On the 'Choose Script Options' screen, there is an option called Script Data which will generate SQL insert statements for all your data.
(Note: for SQL Server Management Studio 2008 R2, the option is called "Types of data to script" and is the last one in the General section. The choices are "data only", "schema and data", and "schema only")
Use bcp (from the command line) to a networked file and then restore it.
e.g.
bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp"
-N -S SOURCESERVERNAME -T
bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T
-N use native types
-T use the trusted connection
-S ServerName
Very quick and easy to embed within code. (I've built a database backup(restore) system around this very command.
You can check the following article to see how you can do this by using both SQL Server native tools and the third party tools: SQL Server bulk copy and bulk import and export techniques
Disclaimer: I work for ApexSQL as a Support Engineer
Hope this helps
You could always export the data from the Customers table to an Excel file and import that data into your Customers table.
To import/export data:
Right click on database
Go to Tasks
Go to Import Data or Export Data
Change the data source to Microsoft Excel
Follow the wizard
If both databases resides in the same instance of SQL Server, ie use same connection, this SQL might be helpful:
INSERT INTO [DestinationDB].[schema].[table] ([column])
SELECT [column] FROM [OriginDB].[schema].[table]
GO
For Data Expoer as SQL script in SQL server 2005,
http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/
I just like to add some screen shoots for Sql Server Management Studio 2008. It is correct to use the steps describe previously. When you have the 'Generate and Publish Script' -> 'Set Script Options' then press Advance to see script options:
![Where to find Advanced script options]: image missing because I do not have the right reputation :(
For Sql Server Management Studio 2008 the option to included data is 'Types of data to script'
![Types of data to script]: image missing because I do not have the right reputation :(