I have recently started using SQL Server 2016 and I'm using SESSION_CONTEXT values to pass some data around.
I'm trying to find out if there is any way to read all session context settings in one. To clarify this is for debugging purposes only - I can already access individual settings, (see code below). I would like to be able to read all such settings in one go if possible.
-- What I Have
EXEC sp_set_session_context 'SortOrder','Price ASC'
EXEC sp_set_session_context 'ItemsPerPage',20
SELECT SESSION_CONTEXT(N'SortOrder') [SortOrder]
SELECT SESSION_CONTEXT(N'ItemsPerPage') [ItemsPerPage]
SELECT SESSION_CONTEXT(N'NotSetYet') [NotSetYet]
-- What I'd like
SELECT * FROM SESSION_CONTEXT_TABLE
Any help gratefully received.
Many Thanks.
Session Context Stored in System pages so you can not retrieve the list of all context without having the key.
If you are looking for global parameters try to save as JSON, XML or CSV string and retrieve with the key.
Related
I'm trying to get into SQL using Microsoft SQL Server Management Studio. I've added a database and I want to make a query:
This works and I can execute the code, but after I saved my query and closed the program it doesn't work anymore when I open the program and try to execute the query again. It can't find the terms I'm relating to:
I don't know why this occurs or how I can solve it, it seems that the connection between the query and the database is gone... Can someone help me on this?
You're attempting to execute the query against the master database but that's not where your tables are. Three ways to handle this:
Use the drop-down in the toolbar to switch to the dbArtemis database
Fully-qualify your table names. dbArtemis.dbo.Klantnummer for example
Execute use dbArtemis; in your query window before the query itself.
Just add before your query the name of your database:
USE dbArtemi
GO
SELECT Naam
FROM tblklaten
WHERE klatenummer =
(SELECT DISTINCT klatnummer FROM tblorders where (orderID = 11013));
GO
I am debugging a stored procedure in Sql Server, I can see the local variables in "Locals", I can add other variables in "Watches" (I have embedded a picture here with Sql Server in debug mode and different debug windows).
My question is: where can I see the result of the select statements during debugging? It is really helpful to see them as they are executed, more so when they read from temporary tables sometimes, which are local to that procedure.
Later edit:
I have followed the advice given below and am having this problem with XML viewer (please see attachment): "The XML page cannot be displayed"
From View contents of table variables and temp tables in SSMS debugger:
This won't be in immediately, but we are considering a feature similar
to this for a future release.
And workaround (you need to add additional XML variable for each temp table):
Declare #TableVar_xml xml
Set #TableVar_xml = (Select * from #TableVar for XML Auto, Elements xsinil);
Then I can look at the table variable contents using the XML viewer.
I have a data flow working just fine, it is compound of a source that is evaluated by a lookup component and then it does an upsert, diagram is shown here:
Now, on BanqueCIBI (the ole db source), I have a SQL Command Text where I would like to receive a param from another component to use it as valueDate. This is the query right now:
SELECT [IdTransactionType]
,[IdBank]
,[IdBanqueDetailHistoryRef]
,[IdBanqueDetail]
,[IdBanqueHeader]
,[CCI]
,[ValueDate]
,[Text]
,[Reference]
,[Amount]
,[Sign]
,[IdCurrency]
,[OrigBranch]
,[dtCreatedOrModified]
,[oldText]
,[oldReference]
,[IdAccount]
,[IdSubAccount]
,[Date]
,[IdRD]
,[Flag]
,[History]
,[DtDate]
,[iTIB]
,[iSAP]
FROM [dbCibi3].[dbo].[BanqueDetailHistoryRef]
WHERE [ValueDate] = '2015-31-01'
So, the diagram would look something like this:
Right now, that new OLE DB Command looks like this:
And this is the usp_GetDateParamsSSIS invoked in the source above:
USE [dbMODIFE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_GetDateParamSSIS]
#name VARCHAR(50) = NULL,
#value DATETIME OUTPUT
AS
BEGIN
SELECT TOP 1 #value =valueDate FROM helperPARAMS_SSIS WHERE name = #name;
END
So, how could I use that #value OUTPUT on the BanqueCIBI component? Thank you so much! (Please notice that BanqueCibi and the new component are querying different servers and a linked served is not an option because of company's policies).
Ok, since you are passing a hard-coded Name parameter to your stored procedure, I am assuming that this is a stored procedure you only need to call once for each execution of the package, and not something you're calling once for every row in your data source.
In that case, do NOT call the stored proc with an OLE DB Command in the data flow.
Instead, call it with an Execute SQL Task that you put BEFORE the DataFlow Task in the Command Flow. Direct the return value of the proc to a package-level variable.
Then in the Source of your dataflow (BanqueCIBI), map that variable to the first parameter of your SELECT query.
There are examples of all of these techniques easily available on the internet. But if you find one you are having trouble following, feel free to edit your question with the details, or create a new question if it is sufficiently different in scope from this one.
I have a large set of pre-existing sql select statements.
From a stored procedure on [Server_A], I would like to execute each of these statements on multiple different SQL Servers & Databases (the list is stored in a local table on [Server_A] , and return the results into a table on [Server_A].
However, I do not want to have to use fully qualified table names in my sql statements. I want to execute "select * from users", not "select * from ServerName.DatabaseName.SchemaName.Users"
I've investigated using Openrowset, but I am unable to find any examples where both the Server name and DatabaseName can be specified as an attribute of the connection, rather than physically embedded within the actual SQL statement.
Is Openrowset capable of this? Is there an alternate way of doing this (from within a stored procedure, as opposed to resorting to Powershell or some other very different approach?)
The inevitable "Why do I want to do this?"
You can do it (specify the server and database in the connection
attributes and then use entirely generic sql across all databases) in
virtually every other language that accesses SQL Server.
Changing all my pre-existing complex SQL to be fully qualified is a
huge PITA (besides, you simply shouldn't have to do this)
This can be done quite easily via SQLCLR. If the result set is to be dynamic then it needs to be a Stored Procedure instead of a TVF.
Assuming you are doing a Stored Procedure, you would just:
Pass in #ServerName, #DatabaseName, #SQL
Create a SqlConnection with a Connection String of: String.Concat("Server=", ServerName.Value, "; Database=", DatabaseName.Value, "; Trusted_Connection=yes; Enlist=false;") or use ConnectionStringBuilder
Create a SqlCommand for that SqlConnection and using SQL.Value.
Enable Impersonation via SqlContext.WindowsIdentity.Impersonate();
_Connection.Open();
undo Impersonation -- was only needed to establish the connection
_Reader = Command.ExecuteReader();
SqlContext.Pipe.Send(_Reader);
Dispose of Reader, Command, Connection, and ImpersonationContext in finally clause
This approach is less of a security issue than enabling Ad Hoc Distributed Query access as it is more insulated and controllable. It also does not allow for a SQL Server login to get elevated permissions since a SQL Server login will get an error when the code executes the Impersonate() method.
Also, this approach allows for multiple result sets to be returned, something that OPENROWSET doesn't allow for:
Although the query might return multiple result sets, OPENROWSET returns only the first one.
UPDATE
Modified pseudo-code based on comments on this answer:
Pass in #QueryID
Create a SqlConnection (_MetaDataConnection) with a Connection String of: Context Connection = true;
Query _MetaDataConnection to get ServerName, DatabaseName, and Query based on QueryID.Value via SqlDataReader
Create another SqlConnection (_QueryConnection) with a Connection String of: String.Concat("Server=", _Reader["ServerName"].Value, "; Database=", _Reader["DatabaseName"].Value, "; Trusted_Connection=yes; Enlist=false;") or use ConnectionStringBuilder
Create a SqlCommand (_QueryCommand) for _QueryConnection using _Reader["SQL"].Value.
Using _MetaDataConnection, query to get parameter names and values based on QueryID.Value
Cycle through SqlDataReader to create SqlParameters and add to _QueryCommand
_MetaDataConnection.Close();
Enable Impersonation via SqlContext.WindowsIdentity.Impersonate();
_QueryConnection.Open();
undo Impersonation -- was only needed to establish the connection
_Reader = _QueryCommand.ExecuteReader();
SqlContext.Pipe.Send(_Reader);
Dispose of Readers, Commands, Connections, and ImpersonationContext in finally clause
If you want to execute a sql statement on every database in a instance you can use (the unsupported, unofficial, but widely used) exec sp_MSforeachdb like this:
EXEC sp_Msforeachdb 'use [?]; select * from users'
This will be the equivalent of going through every database through a
use db...
go
select * from users
This is an interesting problem because I googled for many, many hours, and found several people trying to do exactly the same thing as asked in the question.
Most common responses:
Why would you want to do that?
You can not do that, you must fully qualify your objects names
Luckily, I stumbled upon the answer, and it is brutally simple. I think part of the problem is, there are so many variations of it with different providers & connection strings, and there are so many things that could go wrong, and when one does, the error message is often not terribly enlightening.
Regardless, here's how you do it:
If you are using static SQL:
select * from OPENROWSET('SQLNCLI','Server=ServerName[\InstanceName];Database=AdventureWorks2012;Trusted_Connection=yes','select top 10 * from HumanResources.Department')
If you are using Dynamic SQL - since OPENROWSET does not accept variables as arguments, you can use an approach like this (just as a contrived example):
declare #sql nvarchar(4000) = N'select * from OPENROWSET(''SQLNCLI'',''Server=Server=ServerName[\InstanceName];Database=AdventureWorks2012;Trusted_Connection=yes'',''#zzz'')'
set #sql = replace(#sql,'#zzz','select top 10 * from HumanResources.Department')
EXEC sp_executesql #sql
Noteworthy: In case you think it would be nice to wrap this syntax up in a nice Table Valued function that accepts #ServerName, #DatabaseName, #SQL - you cannot, as TVF's resultset columns must be determinate at compile time.
Relevant reading:
http://blogs.technet.com/b/wardpond/archive/2005/08/01/the-openrowset-trick-accessing-stored-procedure-output-in-a-select-statement.aspx
http://blogs.technet.com/b/wardpond/archive/2009/03/20/database-programming-the-openrowset-trick-revisited.aspx
Conclusion:
OPENROWSET is the only way that you can 100% avoid at least some full-qualification of object names; even with EXEC AT you still have to prefix objects with the database name.
Extra tip: The prevalent opinion seems to be that OPENROWSET shouldn't be used "because it is a security risk" (without any details on the risk). My understanding is that the risk is only if you are using SQL Server Authentication, further details here:
https://technet.microsoft.com/en-us/library/ms187873%28v=sql.90%29.aspx?f=255&MSPPError=-2147217396
When connecting to another data source, SQL Server impersonates the login appropriately for Windows authenticated logins; however, SQL Server cannot impersonate SQL Server authenticated logins. Therefore, for SQL Server authenticated logins, SQL Server can access another data source, such as files, nonrelational data sources like Active Directory, by using the security context of the Windows account under which the SQL Server service is running. Doing this can potentially give such logins access to another data source for which they do not have permissions, but the account under which the SQL Server service is running does have permissions. This possibility should be considered when you are using SQL Server authenticated logins.
I am trying to run the SQL Transport Schema Generation Wizard against a SQL 2012 server. The stored procedure has a datetime parameter. If i simply put in a date like 12/26/2013 05:00:00 Am, then the "Generate" button doesn't show an argument. If i try putting the date/time in a single quote or using a string like 2013-12-26T05:00:00.000, the parameter is generated, but i get the following error when i try to execute. "Failed to execute SQL Statement. Please ensure that the supplied syntax is correct. "
I got to this point by creating a SQL Query that output it's response using FOR XML AUTO, ELEMENTS in it. I then open my BizTalk solution in VS 2012, Go to "Add Items -> Add Generated Items". Select Add Adapter Metadata. From there, it asks the location of the message box. I use my local server. It then asks for the connection string for the SQL Server with the stored procedure. I enter that (it's the same as the server with the message box). I specify the namespace and the root element name for the document. This is set as a receive port. I next select stored procedure and move to the next screen. I then select the stored proc from a drop down list. Below, in a grid, i am shown the parameters for the stored proc. Here is where i am having trouble. I cannot seem to get it to accept the datetime argument no matter what i put in here.
Is there something i am doing wrong?
It is better to do the following steps and to use the new WCF-SQL rather than the old deprecated SQL adapter.
Add Items
Add Generated Items
Consume Adapter Service
Select sqlBinding and Configure the URI
Click Connect
Select Client (Outbound operations)
Select Strongly-Type Procedures
Select the Stored Procedure from Available categories and operations
Click Add
Give it a Filename Prefix
Click OK
This will generated the schemas plus binding files to create the port.
You also don't need to have the FOR XML AUTO, ELEMENTS in your stored procedure any more.