USE Statement in SQL Server - sql-server

I am currently using USE [databaseName] in my stored procedures. However, is there a way that I can point this at the current local database and not give a database name? I know this may sound a bit obscure but its highly likely that I will be using these Stored Procedures in many different databases.

remove the USE [databaseName] from your stored procedure.
By default, the stored procedure will run on the current database (in which SP is created)

Related

SQL Server stored procedure - designate database to use

I am tasked with creating a stored procedure from a script I wrote which will update tables with data.
Since I run this script against a dev database and a live database, we have always manually changed the USE DATABASE in the script before running.
I am looking for a way to use USE DATABASE within a stored procedure.
Is this possible without having to create two stored procedures of the same script for each database (dev versus live)?
Assuming you mean MS SQL Server. You cannot use USE ... within a stored procedure, but you can directly reference a different database with fully qualified object notation.
database.schema.objectname
Example:
dev_mydb.dbo.MyTable
Do note though that if you need that database name to be variable then you will need to use dynamic SQL to set the dbname.

CLR Stored Procedure v regular SQL Stored Procedure

I have been writing a CLR stored procedure that moves data from one database to another. I went with the CLR stored procedure because I like the .NET framework's ability to connect to remote servers better than I like linked servers, or openrowset, but I now find that my class is mostly embedded SQL strings. I was considering just using the CLR stored procedures to retrieve the data onto the local SQL Server, and then using a regular SQL stored procedure for the actual inserts and updates.
I'm not worried about pre-compilation of the procedure or performance, and I do like that the CLR procedure allows me to see all of the logic in one place, read from top to bottom.
Are there any reasons I should consider moving to a TSQL solution instead of CLR?
Thanks.
There are multiple reasons why you would stick to a regular stored procedure. I'll try to give you an overview of the ones that I know of:
Performance.
Memory issues. SQL Server only operates with its own max memory settings. CLR's go out of this bound. This could comprimise other applications (and the OS) running on this server.
Updatebility. You can update a Stored procedure with a simple script. CLR's are more complicated to update
Security. CLR's often require more security settings than regular t-sql.
As a general rule you only want to use CLR for:
interaction with the OS, such as reading from a file or dropping a message in MSMQ
performing complex calculations, especially when you already have the code written in a .NET language to do the calculation.

solution instead of stored procedure in SQL Server 2008

I have a database which has many stored procedure which execute in the first of month. They should read based on rules from some tables and insert calculated results on other table. There are huge number of queries. Can I find another better solution instead of stored procedures?
SQL Server 2008
If you really wanted to you could write a service (e.g. windows service) to do the work but I would suggest stored procedures are probably best. This of course does depend on what the procedures do.

Is it possible to run a SQL Server view from a stored procedure?

I have a TON of views that are rather long and dependent/depended on other views. I also have some stored procedures that need to be run weekly and these procedures need to use some of the SQL statements in the views so instead of putting the statements in the procedures I was hoping I could just run the view.
Is that possible? I'll be running these from SQL Server 2008 btw
SQL 101:
A Select statement can be run on a table or view.
So, yes, any code that can access at able can also access a view. Given the necessary permissions.
As I said - this is beginner SQL knowledge. I will add it to my list of interiew questions for junior developers.
A stored procedure is, in essence, nothing more than a canned ad-hoc statement. Yes, it offers many other features but the point is anything you can type in an interactive query window can also be run in a stored procedure.
Views are not restricted to being run in a stored procedure.
Yes you can query tables and views inside your stored procedure.

Dynamic access to tables from another database inside an user function

I have an user defined table function in SQL Server that aggregate data from several tables including a couple of tables of another database. That is done hardcoding the name of the database in the queries, but we want to make the database name configurable (because our databases usually share the server with the databases of other applications).
I tried to construct a dynamic query string inside the function using the database name that is stored in a configuration table, but:
When I tried exec(#sqlStatement) SQL Server said that execute string is not allowed inside a function.
Then I tried exec sp_executesql #sqlStatement and the function was created, but when you execute it SQL Server says that inside a function you can only run extended functions and procedures.
So the question is: is possible create a function or stored procedure that access a table in another database without having to recreate the function when the database name is different?
TIA.
I would really recommend that you settle on fixed database names, so you can avoid this entire problem of using dynamic sql. Can't you come up with unique database names like: your_company_name_XYZ and your_company_name_ABC? if that is not an option you will be doing lots of dynamic SQL, you should read The Curse and Blessings of Dynamic SQL by Erland Sommarskog
All of the restrictions you mention in the question are a limit of SQL Server functions. You can generate and execute dynamic SQL within a stored procedure with no problems.

Resources