Finding the database name from tsql script - sql-server

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()

Related

End user initiating SQL commands to create a file from a SQL table?

Using SQL Manager ver 18.4 on 2019 servers.
Is there an easier way to allow an end user with NO access to anything SQL related to fire off some SQL commands that:
1.)create and update a SQL table
2.)then create a file from that table (csv in my case) that they have access to in a folder share?
Currently I do this using xp_command shell with bcp commands in a cloud hosted environment, hence I am not in control of ANY permission or access, etc. For example:
declare #bcpCommandIH varchar(200)
set #bcpCommandIH = 'bcp "SELECT * from mydb.dbo.mysqltable order by 1 desc" queryout E:\DATA\SHARE\test\testfile.csv -S MYSERVERNAME -T -c -t, '
exec master..xp_cmdshell #bcpCommandIH
So how I achieve this now is allowing the end users to run a Crystal report which fires a SQL STORED PROCEDUE, that runs some code to create and update a SQL table and then it creates a csv file that the end user can access. Create and updating the table is easy. Getting the table in the hands of the end user is nothing but trouble in this hosted environment.
We always end up with permission or other folder share issues and its a complete waste of time. The cloud service Admins tell me "this is a huge security issue and you need to start and stop the xp_command shell with some commands every time you want generate this file to be safe".
Well this is non-sense to me. I wont want to have to touch any of this and it needs to be AUTOMATED for the end user start to finish.
Is there some easier way to AUTOMATE a process for an END USER to create and update a SQL table and simply get the contents of that table exported to a CSV file without all the administration trouble?
Are there other simpler options than xp_command shell and bcp to achieve this?
Thanks,
MP
Since the environment allows you to run a Crystal Report, you can use the report to create a table via ODBC Export. There are 3rd-party tools that allow that to happen even if the table already exists (giving you an option to replace or append records to an existing target table).
But it's not clear why you can't get the data directly into the Crystal report and simply export to csv.
There are free/inexpensive tools that allow you to automate/schedule the exporting/emailing/printing of a Crystal Report. See list here.

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()

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.

mysqldump Fully Qualifies Triggers with Database Name

I'm am trying to use mysqldump to export a database which needs to be imported using a different database name. Looking at the SQL generated by mysqldump, it appears that triggers are the only object names which are fully-qualified with the source database name thus foiling my needs. Is there anyway to direct mysqldump to not fully-qualify any object names including triggers?
I had the same problem and I found the solution. I was using MySQL Workbench to design my database and I've created some triggers there. All of them used the syntax CREATE TRIGGER trigger_name except for one: CREATE TRIGGER dbname.trigger_name (it was just my mistake). Mysqldump output included all triggers in the same way: only one had database name.
Mysqldump uses your original CREATE TRIGGER instructions which you can see via SHOW CREATE TRIGGER. If you have a trigger defined with a database name, simply replace it (drop and create) with a one without dbname.
Most probably you add the database name when you create the trigger. Try updating your trigger without the database name in it.
Not an ideal solution but pumping the output through the following has gotten rid of the database name on the triggers for me.
mysqldump ... opts ... | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'

Update SQL Server 2005 view with new database name?

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.

Resources