| Today, I have one requirement to check dynamically if a node exists in my xml or NOT. I have a stored procedure that receives XML and I need to check if the message information xml contains one Node or NOT. If that node exists then I need to execute that Stored Procedure by different logic and if not it should run with different logic. I figure it out by using EXISTS. This is my XML, that I got as parameter. Now I need to check if "BulkData" node exists in XML, then I need to write different logic to get the result.DECLARE @ExportData XMLSELECT @ExportData ='<Data Number="A123"><BulkData><EachData Parts="Test1" /><EachData Parts="Test2" /><EachData Parts="Test3" /></BulkData></Data>' So, I used this This will return "1" if node is exists else return "0".SELECT @ExportData.exist('(//BulkData)') That's it. I can write based on the return result by this statement. Let me know if it helps you. Reference : Tejas Shah(http://www.SQLYoga.com) |
Learn SQL and database management at SQLYoga for articles, tutorials, and tips to improve your skills and streamline data operations. Join our community!
August 12, 2009
SQL SERVER: Check if Node exists in XML or not
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
July 31, 2009
SQL SERVER: Use CONTEXT_INFO
CREATE TABLE tblA( ID INT IDENTITY, ColVal VARCHAR(100) )
CREATE PROC TestA
AS
BEGIN
INSERT INTO tblA(ColVal)
SELECT 'Allow To insert'
END
CREATE PROC TestB
AS
BEGIN
DECLARE @UID VARBINARY(128)
SELECT @UID = CAST('TestB' AS VARBINARY(128))
SET CONTEXT_INFO 0x5465737442
INSERT INTO tblA(ColVal)
SELECT 'Not Allow To insert'
END
CREATE TRIGGER trg_TblA
ON tblA
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Message varbinary(128)
SELECT @Message = cast('TestB' as varbinary(128))
IF @Message = CONTEXT_INFO() BEGIN
RAISERROR('Not Allowed to Insert/Update/Delete from SP: TestB',15,1)
ROLLBACK TRAN
END
END
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
July 15, 2009
SQL SERVER: Reset Setup Values, when SQL SERVER is started/restarted
| We have a requirements to clear all setup values when SQL SERVER is started/restarted and we need to setup default values to setup table. I found one Stored Procedure provided by MS SQL SERVER. Let me share it with all of you. SQL SERVER provides this SP: “sp_procoption”, which is auto executed every time when SQL SERVER service has been started. I found this SP and it helps me to figure it out the solution for the request as following way. Let me show you how to use it Syntax use this SP: EXEC SP_PROCOPTION @ProcName, should be Stored procedure name which should be executed when SQL SERVER is started. This stored procedure must be in “master” database. @OptionName, should be “startup” always. @OptionValue, this should be set up to execute this given sp or not. If it is “true/on”, given sp will be execute every time when SQL SERVER is started. If it is “false/off”, it will not. That’s it, lets take an example. I have one Database called Test, I have created setup table: CREATE TABLE SetupTable( Lets insert some default values to this table: INSERT INTO SetupTable VALUES('A') What I need to do is, I need to wipe out this values when SQL SERVER is started and fill it with the same default values, because these values might be updated by application.So, I created one stored procedure in master database, named, CREATE PROC ClearAllData and set up this stored procedure as auto executed every time when SQL SERVER is started as: EXEC SP_PROCOPTION Now, restart SQL SERVICES, and you find that old values will be deleted and new values with ‘X’, ‘Y’, and ‘Z’ will be inserted automatically. If now you want to stop it to execute automatically, we just need to execute this with “false” as: EXEC SP_PROCOPTION I hope this is very clear to use this feature. Reference : Tejas Shah (http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
May 12, 2009
SQL SERVER: Configure Database Mail with SQL SERVER 2005
We used Database mail to send mail to client on each updates.
This is a very simple process to configure. Let me share how to configure Database mail with sql server 2005 with all of you.
After setting up Profile and Account properly, you just need to write following code to send a mail to client:
Step 1:
Step 2:
Step 3:
Step 4: You might get this message:
Step 5: Create Profile
Step 6 : Create Account
That's it.
exec msdb.dbo.sp_send_dbmail
@profile_name = 'ProfileName', IN our CASE, 'Tejas'
@recipients = 'Client Email Address' ,
@blind_copy_recipients = 'BCC Address',
@subject = 'Subject',
@BODY = 'Message Body',
@body_format = 'Message Type', it could be text OR html
Let me know if you have any complexity or comments in setting up Database mail.
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
May 4, 2009
SQL SERVER: Read values from Comma Separated variable
SET @xmlIDs = '' ' + REPLACE(@str, ',', ' ') + ' ' + '
1 6 7 8 20
SELECT x.v.value('.','INT')
FROM @xmlIDs.nodes('/IDs/ID') x(v)
CREATE PROC Test_ReadValuesFromCommaSeparatedVariable
@str VARCHAR(100)
AS
DECLARE @XmlIDs XML
SET @xmlIDs = '
' + REPLACE(@str, ',', ' ') + ' ' +
' '
UPDATE TableName
SET Flag = 1
WHERE ID IN(
SELECT x.v.value('.','INT')
FROM @xmlIDs.nodes('/IDs/ID') x(v)
)
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
April 26, 2009
SQL SERVER - Query to compare number of Rows between different Databases
| We have two databases in the same SQL server instance. Both of the databases are copy of the production database, so both contains same tables. We added some records in Test Database’s some tables to update some features. We have added data in many tables of test database. Now we need to also update Production DB with the updated data. To update Production Database, we need to make sure in which tables we have updated data and then we will check and update production database accordingly. We have many numbers of tables, and we have updated many tables, so its not possible for us to check it manually. So, we need to make query to compare rows of each table with the another database tables, to find out which tables has different rows then the original database. Solution: As I need to solve this problem, I write a query which will give me Rows of each table in one Database. As I need to compare it with another database I write the following query to come out with the solution. Example: I have two databases. Database A and Database B. I need to make a report, which will give me details of each table and rows. I made this Stored Procedure in Database A. CREATE PROC CompareRowsBetweenDatabas This will give me results as I need. Let me know if it helps you. |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
April 20, 2009
SQL SERVER: Get Result ORDER BY Time regardless Date on SQL DateTime column
Usually we save Time with Dates in DATETIME column.
Today, I came across situation, where I need to sort my result set by Time, regardless the Date.
I have some sample data like this:
DECLARE @Data TABLE(dt DATETIME)
INSERT INTO @Data(dt)
SELECT '2008-12-05 04:00:00.000'
UNION ALL
SELECT '2008-12-10 10:00:00.000'
UNION ALL
SELECT '2009-03-01 08:00:00.000'
UNION ALL
SELECT '2009-03-02 07:15:00.000'
UNION ALL
SELECT '2009-03-10 08:50:00.000'
UNION ALL
SELECT '2008-12-31 23:00:00.000'
UNION ALL
SELECT '2009-05-01 21:10:00.000'
SELECT * FROM @Data
I found very quick solution for this. You can create query as follows:
SELECT *
FROM @Data
ORDER BY Convert(VARCHAR, dt,108)
Let me know if it helps you in any way.
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
April 15, 2009
SQL SERVER: Difference between OpenQuery and OpenRowSet
Today, one developer asked me what is the difference between OpenQuery and OpenRowSet.
Let me share this thing with all of you.
Syntax for both the command:
OPENQUERY ( linked_server ,'query' )
OPENROWSET
( 'provider_name' , 'datasource' ; 'user_id' ; 'password'
, { [ catalog. ] [ schema. ] object | 'query' }
)
Difference is:
OpenQuery uses a predefined linked server,
While OpenRowSet has to specify all the connection options. So with OpenRowSet you can query to your remote SQL server from local.
Else it's the same.
Let me know if it helps you in any way.
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
April 14, 2009
SQL SERVER: SQL Query To Find Most used Tables
| We have very large database and today we want to search the tables which are used mostly. Means tables which are used in Procedures, Constraints, Views, Triggers etc. I know this is very strange requirement, but we need to do this. So, I tried to make an query which will help me to find out the top most tables used in other objects as I mentioned Let me share that sp with all of you: SELECT TableName, COUNT(*) So, I made my life easy, by using this. I can get the list if Tables which are used most. Let me know if it helps you in any way. |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
April 2, 2009
SQL SEVER: How to make an Entry in Event Viewer using SQL SERVER
| Today I came across the situation where I need to following scenario. I need to make entry in Event Viewer when there is an error in Stored Procedure. I wondered to do this, but finally I come up with the solution. SQL is much powerful. SQL provides us to make an entry in Event Viewer by two ways: 1. using XP_LogEvent 2. By Raiserror WITH LOG Lets first see the way using XP_LogEvent:Here I have created one SP which will raise an error “Divide by zero error encountered.” as I tried to do “10 / 0”. CREATE PROCEDURE TestEventViewer EXEC xp_logevent 60000, @msg, informational END CATCH Lets Execute this SP: EXEC TestEventViewer This will write entry in Event Viewer. Now open Event Viewer. You can find Event Viewer at Control Panel –> Administrative Tools –> Event Viewer. You will get en entry of Error there. So we can do this by Extended Stored Procedure: “xp_logevent”. Let see the parameters of this Procedure. First Parameter: “60000” is the Error Number Second Parameter: “@msg” is the message to be displayed in Event Viewer. Third Parameter: “informational” is the Error Level. It could be “informational”, “Error”, “Warning”. Now, Lets see by another way By Raiserror WITH LOG: It is the same way as we used Raiserror to Raise an Error. CREATE PROCEDURE TestEventViewer RAISERROR(@msg, 11, 1) WITH LOG END CATCH Lets Execute this SP: EXEC TestEventViewer. So By these ways we can make an entry to Event Viewer. Let me know if it helps you in any way |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS