How to get the path of a directory of a script using sql query?. The SQL query is run by same script - sql

I have a SQL query which is running by script while I am publishing the code in .net,
My problem is that, I need a SQL query to find the directory of same script.
Please help me to do so.

I don't think that the SQL Server client cares where the script is being executed from. Your best bet would be to figure out what the path of the script is before you execute it and pass it in as a parameter, or embed it in the script itself.

Related

Is it possible to get Azure database region is sql script?

In Azure database, ServerProperty can get some server property information. But, I want to know the region which database server is running. Is it possible? Can I get this region information with SQL script like below (or something like this):
SELECT SERVERPROPERTY('Region')
Not sure if this can be done from Sql out of the box, but one indirect way could be by using powershell and then calling that powershell script from sql script.
Get-AzureRmSqlServer powershell cmdlet gives detailed information about the servers including region. (Ref)
Then you can call this PS script from SQL as described here.
Not tried it but hope it should work.

Create a Sql Server Db from WIX in a specific location

I'm trying to have WIX create a database in a location the user specifies. I've got it working for the most part using some CustomActions and passing parameters to a sql script. But am having problems with permissions on the directory I'm attempting to create the DB in.
Searching for a resolution to this problem lead me to a post on here that I had not seen before today (Wix: create a sql server database at a specified location) where it seems like someone accomplished the same thing I'm wanting to do.
It is not clear from that post how the user was invoking the script? I am hoping they were doing something like the code code snippet below and I just don't know how to pass values to the script. If so, that would greatly simplify my task.
<sql:SqlDatabase>
<sql:SqlScript>
</sql:SqlDatabase
Can someone tell me how to pass values to a SqlScript when not using a customAction to invoke the script via sqlcmd.exe?
Thanks.
For the future reference of anyone else wishing to do this there are a couple of ways to create a database at a specified location. The simplest of which is using the wix tags that I did not know existed:
<sql:SqlFileSpec Name="DataBaseFile" Id ="mdfFile" Filename = "[DBDIR]TestDataBase.mdf"/>
<sql:SqlLogFileSpec Name="DataBaseLogFile" Id ="ldfFile" Filename = "[DBDIR]TestDataBase_Log.ldf"/>
This works perfectly as long as the directory that the database is being created in is not under "Program Files" or "Program Files (x86)". Both of these locations seem to have special permissions to them that don't allow SQL Server to create a database there. This theory can be tested by trying to create a database using Sql Server Mgmt Studio in a folder somewhere under Program Files and looking at the error that is produced. Which is something like this for me:
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file
I'm still trying to figure out how an installer can accomplish putting a database under Program Files as I'm sure it's been done before.

SQL Script to delete old backups matching a certain naming convention?

I need to have a T-SQL statement that will delete files older than a day in a directory, but only if they match a certain naming convention. There are multipul backup files in this directory, but only one set we need to keep for a day. I'm unable to use a maintenance cleanup task because it will clear out all bak files, instead of only the ones I want.
Will using the dbo.xp_delete allow the user of the wildcard character %?
You should be able to do this through a SQL Server Maintenance Plan. Look at the "Maintenance Cleanup Task". You can clean up files using a variety of criteria, including age and filename.
This task uses the EXEC xp_delete_file statement.
This feature doesn't seem well documented at all. From what you can read in the comments here you get the general idea of the function:
http://sqlblog.com/blogs/andy_leonard/archive/2009/03/11/xp-delete-file.aspx
I would suggest you write a clean-up task outside of the sql server. This can be easily done with a bat-script or a Powershell script. You can execute these scripts with windows tasks or, in case of a Powershell script, even from within SQL Server itself.

Stopping Powershell script to continue after an SQL error inside of the script fails?

I have a PS script which connects to MS SQL and makes changes in the DB and if the SQL script fails, my ps script still continues with the next steps. So I wonder what is a good way to stop the script after it failed on SQL side?
My Powershell didn't recognize the normal abortion from SQL, so is the another way?
If you find a way to check if your SQL script worked or not (if you donĀ“t know how you should provide a little more info on what you are doing), you can use Break to stop your powershell script.
Take a look at $ErrorActionPreference. You can set this at a script level to Stop.
This is basic error handling. See if this help:
http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=3447&m=3443&ct=18350#?fbid=8N34OvP1Y5K

Creating multiple stored procedures from SQL executed by powershell issue

Ok, so I've got a bit of a SQL and Powershell problem. There are 2 SQL scripts, one to setup 4 different global stored procedures. Another to execute them and manipulate data before returning it to PS to be placed in a CSV file. The reason I'm not putting them into a single file is for readability. The procs are enclosing huge chunks of sql and I cannot create permanent procs in our production environment.
The problem I'm running into is the script runs fine in SQL Mgmt Studio but when ran by PS, I get several errors around the 'go's in the script.
I'm pretty sure this is a problem with the format that PS and the .NET classes expect when executing and returning data sets but...I'm at a loss.
I'm running SQL Server 2005 btw.
Any ideas or similar experiences?
What errors do you get? How are you executing each file? GO is a batch separator understood only by certain tools (e.g. Management Studio); PowerShell doesn't know what GO means. Have you tried executing the separate CREATE PROCEDURE scripts without issuing a GO command between them? If they are separate commands this shouldn't be an issue.
"GO" is a delimiter used by SQL Management Studio. It is not a valid SQL keyword. You can configure SQL Management Studio and change "GO" to "ENGAGE" if you wanted to.
Just remove "GO" from the scripts.