Update SQL Server 2005 view with new database name? - sql-server

I have approximately 100 SQL views that are a variation of this:
select * from RTC.dbo.MyTable
...now I find I need to change the name of the RTC table to something else. Rather than edit one view at a time, is there a way to script out all their drop/create statements to a text file so that I can do a global replacement?

In SSMS right click the database, go to Tasks and select there 'Generate Scripts...'. Select 'Views', select the views you want exported, export.

I'd use PowerShell. If you're not using SQL 2008 Client Tools, install them. Then get the PowerShell client, add the registered snapins (plenty of information out there on how to do that), and then use the directory structure to get to the folder representing your Views.
Then script them using something like:
Get-ChildItems | % {$_.Script()}
Use ScriptOptions to tell it to use an Alter script.
And replace "RTC." with the new database name... and run them using sqlcmd.
PowerShell actually becomes a really nice deployment option too.

Related

Is there a pushd/popd for the USE database statement?

I'm writing a SQL script which is executed as part of a series of SQL scripts. I cannot access the other scripts nor do I have control over the "series-execution"-logic.
I want to change the database within my script (USE someDB), however, I want to make sure that after my script has run the previous DB is the current DB again. Is there some kind of pushd/popd for database usage? An alternative, e.g., by somehow writing the current DB into a temporary variable?
I don't know about any pushd/popd functionality, but the way to get the current database name is use the DB_NAME() function like this:
SELECT DB_NAME()

How to store queries in SQL Server database?

I manage a mini database and I write procedures for complex transactions and data cleansing. I also do a lot of ad-hoc querying and I save all of my queries in a folder. Is there any way I can save these queries in the database so that some of my peers can review my SQL queries?
In my search, I understand that I can write a procedure for smaller queries too. But I want to know if there is another method to do this?
For a select statement use a view:
CREATE VIEW MyView
AS
SELECT Columns FROM TABLE
Now you can select from that
SELECT * FROM MyView
and join to it:
SELECT * FROM MyView
INNER JOIN SomethingElse
ON MyView.ID = SomethingElse.ID
For scripts that update/delete/insert or do procedural things in order, use a stored procedure instead.
You can have queries (views) persisted in the database itself. You can use the CREATE statement to create views, stored procedures, table value functions etc. which will be accessible through intelisense and show up in the database object tree
You can create a new folder in the Template Browser and add code in new templates. If you want to share these ACROSS your team using SSMS, you can also do the following: You DO have to store the code elsewhere, but it can be accessed within SSMS by all users when set up this way on their machines:
See: https://www.sqlservercentral.com/articles/ssms-shared-sql-templates
Short synopsis:
Store code examples in central location and re-point SQL templates folder on each user machine to the central location, by using mklink to create new link to SQL folder under the following location so that the SQL folder will no longer point to it, but to the alternate central location path specified:
C:\Users\YourUserName\AppData\Roaming\Microsoft\SQL Server Management Studio\11.0\Templates
To do this open the command prompt and:
go to user path above and rename SQL folder found under there: ren Sql Sql_Old
create symbolic link: mklink /D Sql C:\ss\Internal\Code\TSQL\SSMS_Templates
if successful, you will see:
symbolic link created for Sql <<===>> path of central code
Afterwards, the template browser will link to the central location and show whatever is in there.

Is there a way to open a sql file in SQL Server Management Studio?

I have thousands of stored procedures in my data base. And I can't stand scrolling through the entire list to find the SPROC I'm looking for. Is there a command in sql server mgmt studio to open the file in the editor like 'OPEN dbo.SomeStoredProcedureName'
There is no T-SQL command to do this, as SSMS is just a client management tool. Your best bet is to use the Filter tool built in to SSMS:
I don't believe there is such command but if you just want to see and not update you could use
sp_helptext 'dbo.SomeStoredProcedureName'
This messes up the formatting so you shouldn't use it to update (while you can). I also map a key to it Ctrl-F1 that can be used to just write the name and select an d press Ctrl-F1.
You've got lots of options, depending on exactly what you're trying to do:
You can "open file" and graphically browse to the directory you want, then graphically scroll down to and open the file you want.
You can "use" the database you want, and "exec" the stored procedure you want from a command-line query window.
You can "exec sp_helptext XXX" the stored procedure to see the text in a command-line query window.
You can use "filter" in the GUI to eliminate stuff you don't want to see
You can write a VBScript or Powershell script to do the same stuff the SSMS GUI lets you do.
Etc etc
You could export the whole database to a creation script. Then it would be easy to search in your favorite file viewer.
From Management Studio you can right-click on the database, choose Tasks->Generate Scripts, and then select "Stored procedures". If you're just browsing, this is very handy. Also good to store such scripts in your code repository.
SqlSmash lets you navigate easily to any object (including stored procedures) in SSMS.
Source
Disclaimer: I am the developer for the addin.

Finding the database name from tsql script

Is it possible to programmatically find the database context from a tsql script? ie the context that changes when you add a USE . I ask because I am not using a USE, and would like to find the database name the script is running on.
select db_name()

How can I use a SQL Scripts in a Database Project with the System.Data.SQLite data provider?

I've got a project where I'm attempting to use SQLite via System.Data.SQLite. In my attempts to keep the database under version-control, I went ahead and created a Database Project in my VS2008. Sounds fine, right?
I created my first table create script and tried to run it using right-click->Run on the script and I get this error message:
This operation is not supported for the provider or data source you are using.
Does anyone know if there's an automatic way to use scripts that are part of database project against SQLite databases referenced by the databases, using the provider supplied by the System.Data.SQLite install?
I've tried every variation I can think of in an attempt to get the script to run using the default Run or Run On... commands. Here's the script in it's most verbose and probably incorrect form:
USE Characters
GO
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
BEGIN
DROP Table Skills
END
GO
CREATE TABLE Skills
(
SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
SkillName TEXT,
Description TEXT
)
GO
Please note, this is my first attempt at using a Database, and also the first time I've ever touched SQLite. In my attempts to get it to run, I've stripped any and everything out except for the CREATE TABLE command.
UPDATE: Ok, so as Robert Harvey points out below, this looks like an SQL Server stored procedure. I went into the Server Explorer and used my connection (from the Database project) to get do what he suggested regarding creating a table. I can generate SQL from to create the table and it comes out like thus:
CREATE TABLE [Skills] (
[SkillID] integer PRIMARY KEY NOT NULL,
[SkillName] text NOT NULL,
[Description] text NOT NULL
);
I can easily copy this and add it to the project (or add it to another project that handles the rest of my data-access), but is there anyway to automate this on build? I suppose, since SQLite is a single-file in this case that I could also keep the built database under version-control as well.
Thoughts? Best practices for this instance?
UPDATE: I'm thinking that, since I plan on using Fluent NHibernate, I may just use it's auto-persistence model to keep my database up-to-snuff and effectively in source control. Thoughts? Pitfalls? I think I'll have to keep initial population inserts in source-control separately, but it should work.
I built my database using an SQLite SQL script and then fed that into the sqlite3.exe console program like this.
c:\sqlite3.exe mydatabase.db < FileContainingSQLiteSQLCommands
John
Well, your script looks like a SQL Server stored procedure. SQLite most likely doesn't support this, because
It doesn't support stored procedures, and
It doesn't understand SQL Server T-SQL
SQL is actually a pseudo-standard. It differs between vendors and sometimes even between different versions of a product within the same vendor.
That said, I don't see any reason why you can't run any (SQLite compatible) SQL statement against the SQLite database by opening up connection and command objects, just like you would with SQL Server.
Since, however, you are new to databases and SQLite, here is how you should start. I assume you already have SQLite installed
Create a new Windows Application in Visual Studio 2008. The database application will be of no use to you.
Open the Server Explorer by pulling down the View menu and selecting Server Explorer.
Create a new connection by right-clicking on the Data Connections node in Server Explorer and clicking on Add New Connection...
Click the Change button
Select the SQLite provider
Give your database a file name.
Click OK.
A new Data Connection should appear in the Server Explorer. You can create your first table by right-clicking on the Tables node and selecting Add New Table.

Resources