Wednesday, August 29, 2012

Cannot insert the value NULL into column

In one of my projects i have been working on is in its final development phases and QA deployment has begun.

One of the tables with column name last_changed_by gets automatically populated with the session user name on insert or update. The column default value is set to: create default set_to_userid as suser_name()

This works as expected in DEV environment (Windows Server 2008 and SQL Server 2008 R2) and the field is populated with the user performing the DML (insert or update).

In QA (Windows Server 2000 and SQL Server 2000) does not work on the same table and returns error below when Oracle Fusion tries DML:
‘Cannot insert the value NULL into column 'last_changed_by', table 'mysecurity.dbo.security_app_users'

QA environment access is very strict that dev team cannot run any kind of test cases, so helpless we decided to pass suser_sname ()

Its still unclear whats causing the default value not to kick in on DML statement execution. Any input is appreciated....




MDM PDH Trillium and Purisma

My new assignment includes working on MDM (Master Data Management), PDH (Account Matching), Trillium (Address Standardization Tool) and Purisma Data Hub (PDH Development).

This is going to be a great learning experience. KT sessions have already started and my first task will adding new fields to PDH which interacts with SAP and Siebel for account management.

Account Matching: Match new and existing accounts based on default and user defined criteria and gives matching percentage to the user to select the appropriate account.

Will post some new topics soon....

Number of occurrences of space in a given string

I had used regular expression for counting number of occurrences of a character or string in SQL Oracle Database, but didnt know how to derive the same in SQL Server. 

Google search gave me the answer i was looking for:

declare @string varchar(50)
select @string = 'number of occurrences of space in a given string'
SELECT LEN(@string) - LEN(REPLACE(@string, ' ', '')) AS occurrence_count


Not sure what copyright policy the website has from where i got this piece of code, so not going to get into that....


Hope this helps.

Friday, August 17, 2012

SQL Server TEMPDB FULL

Got a new assignment to research why TEMPDB of a SQL Server 2008 64-bit is termination active user and application sessions.

DBA resource suspects a 120 table join is causing the TEMPDB to run out of disk space causing sessions  terminating unexpectedly.





Logged in the database for the first time, reviewed the procedure and tables and views involved. About 10 tables with significantly varying record counts which are joined by PK and other columns. Also the procedure runs in parallel (24 CPU's on the host machine).

Need to figure out the 120 table join, how why and where....

This is going to be a good learning opportunity.



Hopefully we will be able to resolve this TEMPDB issue and some good experience.

Update: Still no update, the users did not want the job running anymore so had to divert my attention towards other tasks.

One thing i suspect is there are 24 CPU's in that server and the job joins 2 tables, 2 views (3 tables in each view AFAIK). Can this be the reason for all those threads running in parallel? MAXDOP is not specified so may be?

Thursday, August 16, 2012

SQL SERVER Object Property

SQL SERVER Object Property

Object properties that i have used frequently. Most of the times i have used object properties is to check whether the object exists or not. For example when deploying an existing procedure to production, the IsProcedure property is handy to check if the procedure exists in production or not. IsScalarFunction can be used to check existence of a function.

If it exists rename it with appropriate date time stamp (never use time delimiter i.e. 12:35:58 in an object name as it can cause problems if you are planning to use SQL Server Replication in future as these object names cannot be handled by the replication agent) and then you can successfully create a fresh copy of the modified procedure and you will have a backup copy as well.

Similarly try using all object properties in your spare time and think of ways  they can be used to make your life at work simple, you only need to be aware of what object properties exist and not remember the syntax (at least i don't) 
OBJECTPROPERTY ( id , property )
IsProcedure
IsScalarFunction
CnstIsClustKey
CnstIsColumn
CnstIsNonclustKey
ExecIsTriggerNotForRepl
IsCheckCnst
IsConstraint
IsForeignKey
IsIndexed
IsPrimaryKey
IsTable

GOOGLE: "sql server msdn objectproperty"

Update: For checking existance of non unique and non clustered indexes i did not find any ObjectProperty. 

USE [myDB]
GO


IF NOT EXISTS (SELECT * FROM sysindexes WHERE name = 'idx_account_id' AND id = OBJECT_ID ('myDB.dbo.account'))
BEGIN
  CREATE NONCLUSTERED INDEX [ idx_account_id ] ON [dbo].[account] 
  (
  [UCN] ASC
  ) ON [PRIMARY]
END

The 2 ObjectPorperties below will not work for non unique and non clustered indexes.

Property nameObject typeDescription and values returned
CnstIsClustKey
Constraint
PRIMARY KEY constraint with a clustered index.
1 = True
0 = False
CnstIsNonclustKey
Constraint
PRIMARY KEY or UNIQUE constraint with a nonclustered index.
1 = True
0 = False

Using APPLY

I had never heard or used this clause previously and just came across an example at work of how APPLY can be used to retrieve data from a join.

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.

There are two forms of APPLY: CROSS APPLY and OUTER APPLY

CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. 

OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function.

Often these operators can be used for retrieving SKU pricing, product SKU's, item lookups, etc. For example (got this as close as i can get without copy pasting from the actual script):

SELECT A.PRODUCT_SKU, A.PRODUCT_SKU_DESCRIPTION, B.SKU_LEVEL
FROM DBO.PRODUCT_SKUS A (NOLOCK)
INNER JOIN DBO.SKU_SALES B (NOLOCK)
ON A.PRODUCT_SKU = B.SKU
AND A.STATUS = 'A'
AND A.DISTI_CHANNEL = '007'
AND B.STATUS = 'A'
CROSS APPLY
(
SELECT TOP 1 FROM DBO.SKU_PRICE_ATTRIBUTE_LOOKUP C (NOLOCK)
INNER JOIN DBO.SKU_PRICE_12 D (NOLOCK)
ON C.SKU = D.SKU_ID
AND C.STATUS = 'A'
AND D.STATUS = 'A'
AND D.LAST_UPD_ROW_ID = '3RD-Q-12'
)

I used the same text as in the help section of MSDN, but this is something i would like to remember to use in future and need to spread the word about.

Wednesday, August 1, 2012

Using OPENROWSET

Using OPENROWSET

Easiet way to load data in a table in SQL Server for the purposes of data analysis, data load or anything you want to.

I know people will argue about using BCP, which is also a very good option. Using BCP you have good control over things like collation when importing data from different character sets or different international data sources and don't have to stick with Kanji.


INSERT dbo.xls_to_table
    (
    [ID],
    [COLUMN_1],
    [COLUMN_2],
    [COLUMN_3],
    [COLUMN_4]
    )
SELECT *
FROM OPENROWSET ( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0; HDR = YES; IMEX = 1; DATABASE = C:\temp.xls '
SELECT ' + CONVERT(nvarchar(255), @ID) + ',
    [COLUMN_1],
    [COLUMN_2],
    [COLUMN_3],
    [COLUMN_4]
FROM [xsl_sheetname$] )