Thursday, September 13, 2012

Usage of sp_executeSQL



Usage of sp_executeSQL

SET @sql = 'Select * into ' + @delim_delta_table + ' from ' + @table_server + '...'   
  + @delim_table_name + ' where ' + @timestamp_col_name + ' > @LastTimeIN'  + ' and '   
  + @timestamp_col_name + ' <= @TimeMax '   
 
     
    select @parmlist = '@LastTimeIN datetime, @TimeMax datetime'   
    exec sp_executesql @sql, @parmlist, @LastTimeIN=@last_time , @TimeMax=@time_max

Thursday, August 30, 2012

DDL trigger

to keep the change log of Database activity for structure DDL trigger at Database level is used.


  1. I ran across some permission issue: where  it is needed to make sure that owner of db where log table resided and source db should be same.
  2. for user's activities to be logged he needs to be member of destination or EventLogDb. (Public Role is Ok)


trigger code:


CREATE TRIGGER [Backup_Objects]

on database
--WITH EXECUTE AS SELF
for
CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
CREATE_TABLE, ALTER_TABLE, DROP_TABLE,
CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
CREATE_ROLE,ALTER_ROLE,DROP_ROLE,
CREATE_SCHEMA,ALTER_SCHEMA,DROP_SCHEMA,
CREATE_TRIGGER,ALTER_TRIGGER,DROP_TRIGGER,
CREATE_VIEW,ALTER_VIEW,DROP_VIEW,
CREATE_USER,ALTER_USER,DROP_USER


as
--Created on Agt 11th 2011 by Abhishek Joshi
--refer:http://www.sqlteam.com/article/using-ddl-triggers-in-sql-server-2005-to-capture-schema-changes
SET NOCOUNT ON

declare @data xml
set @data = EVENTDATA()

insert into eventslogdb.dbo.changelog(databasename, eventtype,
    objectname, objecttype, sqlcommand, loginname)
values(
@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)'),
@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'),
@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
)







GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

Tuesday, July 31, 2012

INSERT INTO VS SELECT INTO PERFORMANCE

INSERT INTO VS SELECT INTO  PERFORMANCE DIFFERENCE


1.  SELECT INTO   hold lock on system table while it is creating table, while for  INSERT INTO there are no such lock as table is already there.  also these lock are hold until the query completes.
     
     these difference become very evident and affecting performance when query execution times are longer. and multiple users are trying to execute same piece of query as later request will have to wait for prior execution to finish before they can obtain lock on the resources again. there wait time can be avoided by creating table first and then inserting data into table that is by using INSERT INTO over SELECT INTO.
For instantaneous queries(small data) this will not be that much of problem.


2.  SELECT INTO is not a logged operation while  INSERT INTO  is

Tuesday, June 26, 2012

Get Top Biggest Table in SQL Database



CREATE TABLE #tmpTable
      (
      [name]      VARCHAR(255),
      [rows]      INT,
      [reserved] VARCHAR(100),
      [data]      VARCHAR(100),
      [index_size]      VARCHAR(100),
      [unused] VARCHAR(100)
      )

--_DEFAULT_VendorJournalAccts 4          16 KB 8 KB  8 KB  0 KB
EXEC sp_MSforeachtable @command1=" Insert into #tmpTable EXEC sp_spaceused '?'"

SELECT TOP 100 * FROM #tmpTable
ORDER BY Data DESC

DROP TABLE #tmpTable

Friday, June 1, 2012

Limitation and Capabilities of MS SQL Server

MS SQL SERVER 2008

Following are interesting facts about MS SQL SERVER


Object
Maximum sizes/numbers SQL Server (32-bit)
Maximum sizes/numbers SQL Server (64-bit)
Bytes per row8
8,060
8,060
Bytes in source text of a stored procedure
Lesser of batch size or 250 MB
Lesser of batch size or 250 MB
Bytes per varchar(max), varbinary(max), xml, text, or image column
2^31-1
2^31-1
Characters per ntext or nvarchar(max) column
2^30-1
2^30-1
Clustered indexes per table
1
1
Columns in GROUP BY, ORDER BY
Limited only by number of bytes
Limited only by number of bytes
Columns or expressions in a GROUP BY WITH CUBE or WITH ROLLUP statement
10
10
Columns per index key7
16
16
Columns per foreign key
16
16
Columns per primary key
16
16
Columns per nonwide table
1,024
1,024
Columns per wide table
30,000
30,000
Columns per SELECT statement
4,096
4,096
Columns per INSERT statement
4096
4096
Connections per client
Maximum value of configured connections
Maximum value of configured connections
Database size
524,272 terabytes
524,272 terabytes
Databases per instance of SQL Server
32,767
32,767
Filegroups per database
32,767
32,767
Files per database
32,767
32,767
File size (data)
16 terabytes
16 terabytes
File size (log)
2 terabytes
2 terabytes
Foreign key table references per table4
253
253
Identifier length (in characters)
128
128
Instances per computer
50 instances on a stand-alone server for all SQL Server editions.
50 instances on a stand-alone server.
SQL Server supports 25 instances on a failover cluster.
25 instances on a failover cluster.
Length of a string containing SQL statements (batch size)1
65,536 * Network packet size
65,536 * Network packet size
Locks per connection
Maximum locks per server
Maximum locks per server
Locks per instance of SQL Server5
Up to 2,147,483,647
Limited only by memory
Nested stored procedure levels6
32
32
Nested subqueries
32
32
Nested trigger levels
32
32
Nonclustered indexes per table
999
999
Number of distinct expressions in the GROUP BY clause when any of the following are present: CUBE, ROLLUP, GROUPING SETS, WITH CUBE, WITH ROLLUP
32
32
Number of grouping sets generated by operators in the GROUP BY clause
4,096
4,096
Parameters per stored procedure
2,100
2,100
Parameters per user-defined function
2,100
2,100
REFERENCES per table
253
253
Rows per table
Limited by available storage
Limited by available storage
Tables per database3
Limited by number of objects in a database
Limited by number of objects in a database
Partitions per partitioned table or index
1,000
1,000
Statistics on non-indexed columns
30,000
30,000
Tables per SELECT statement
Limited only by available resources
Limited only by available resources
Triggers per table3
Limited by number of objects in a database
Limited by number of objects in a database
Columns per UPDATE statement (Wide Tables)
4096
4096
User connections
32,767
32,767
XML indexes
249
249


1Network Packet Size is the size of the tabular data stream (TDS) packets used to communicate between applications and the relational Database Engine. The default packet size is 4 KB, and is controlled by the network packet size configuration option.
2The maximum number of bytes in any index key cannot exceed 900 in SQL Server. You can define a key using variable-length columns whose maximum sizes add up to more than 900, provided no row is ever inserted with more than 900 bytes of data in those columns. In SQL Server, you can include nonkey columns in a nonclustered index to avoid the maximum index key size of 900 bytes.
3Database objects include objects such as tables, views, stored procedures, user-defined functions, triggers, rules, defaults, and constraints. The sum of the number of all objects in a database cannot exceed 2,147,483,647.
4Although a table can contain an unlimited number of FOREIGN KEY constraints, the recommended maximum is 253. Depending on the hardware configuration hosting SQL Server, specifying additional FOREIGN KEY constraints may be expensive for the query optimizer to process.
5This value is for static lock allocation. Dynamic locks are limited only by memory.
6If a stored procedure accesses more than 64 databases, or more than 2 databases in interleaving, you will receive an error.
8SQL Server supports row-overflow storage which enables variable length columns to be pushed off-row. Only a 24-byte root is stored in the main record for variable length columns pushed out of row; because of this, the effective row limit is higher than in previous releases of SQL Server. For more information, see the "Row-Overflow Data Exceeding 8 KB" topic in SQL Server Books Online.


Source:
http://msdn.microsoft.com/en-us/library/ms143432(v=sql.105).aspx

Thursday, May 31, 2012

Execute stored proc to select into a table



SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=;Trusted_Connection=yes;',
     'EXEC database.schema.usp_spName  ''parameter'' ')

SELECT * FROM #MyTempTable

If you get error like
"
Msg 15281, Level 16, State 1, Line 1

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
"

then you need to turn on these options by:

executing following code:

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO


Monday, May 21, 2012

XML string to Dataset

Lately I have worked a lot with SSIS where I am consuming some web services as source and writing to destinations like SQL server or a txt file.

following method is very useful when I use script task to connect to Source and get XML as string and convert it to oledb dataset and loop through each record to write it to destination.


  private DataSet XmlString2DataSet(string xmlString)
        {
            //create a new DataSet that will hold our values
            DataSet quoteDataSet = null;

            //check if the xmlString is not blank
            if (String.IsNullOrEmpty(xmlString))
            {
                //stop the processing
                return quoteDataSet;
            }

            try
            {
                //create a StringReader object to read our xml string
                using (StringReader stringReader = new StringReader(xmlString))
                {
                    //initialize our DataSet
                    quoteDataSet = new DataSet();

                    //load the StringReader to our DataSet
                    quoteDataSet.ReadXml(stringReader);
                }
            }
            catch
            {
                //return null
                quoteDataSet = null;
            }

            //return the DataSet containing the stock information
            return quoteDataSet;
        }