How to: create login with 'sa' credentials

CREATE LOGIN loginname
WITH PASSWORD = 'changeme' MUST_CHANGE
,CHECK_EXPIRATION = ON
,DEFAULT_DATABASE = DBName
GO
EXECUTE sp_addsrvrolemember
@loginame = 'loginname',
@rolename = 'sysadmin'

How to: assign IDs/SortVal values using ROW_NUMBER()

I have a lookup table L_MyLookup with columns ID and NAME.
Later, my client wanted a SORTVAL column because they want to define the order of the values through that column instead of ID.

So
  1. add the SORTVAL column
  2. update the values
  3. alter SORTVAL to become not nullable
Like the following code:


ALTER TABLE L_MyLookup

ADD SORTVAL int NULL

go

UPDATE L_MyLookup set SORTVAL = temp.SORTVAL

FROM

(SELECT ID, ROW_NUMBER() over (order by ID) as SORTVAL

FROM L_MyLookup) as temp

WHERE temp.ID = L_MyLookup.ID

go

ALTER TABLE L_MyLookup

ALTER COLUMN SORTVAL int NOT NULL

FTS: Create new Catalog

See: how to drop existing FTS Catalog

EXEC sp_fulltext_database 'enable'
go

CREATE FULLTEXT CATALOG MyFTSCatalog
go

CREATE FULLTEXT INDEX ON MyTable
(
Document
TYPE COLUMN DocumentExtension
LANGUAGE 0x0407 --this is German
)
KEY INDEX PK_MyTable
ON MyFTSCatalog
go

--to populate the catalog,
--do the following when traffic is light:
ALTER FULLTEXT INDEX ON Production.Document SET CHANGE_TRACKING AUTO;
GO




FTS: Drop Catalog

sp_fulltext_table 'MyTable', 'drop'
go
sp_fulltext_database 'enable'
go
sp_fulltext_catalog 'MyFTSCatalog', 'drop'
go
sp_fulltext_service 'clean_up'
go

SSIS auto generate update scripts

Specify the TableName in @TableName variable to create the Update script to be used for SSIS.

DECLARE @counter int;

DECLARE @QueryString nvarchar(2400) = ''

DECLARE @ColumnName nvarchar(120);

DECLARE @TableName nvarchar(120) = 'MyTable'

CREATE TABLE #ColumnList

(

ID int IDENTITY(1,1)

,ColumnName varchar(1200)

)

INSERT #ColumnList

select sys.columns.name from sys.columns

inner join sys.tables on sys.tables.object_id = sys.columns.object_id

where sys.tables.name = @TableName

and sys.columns.name <> 'ID'

SET @counter = 1

SET @QueryString = 'UPDATE dbo.' + @TableName + '

SET '

SET @ColumnName = (SELECT ColumnName FROM #ColumnList WHERE ID=@counter)

SET @QueryString = @QueryString + '

' + @ColumnName+ ' = ? '

SET @counter = @counter + 1;

WHILE @counter <= (SELECT COUNT(*) FROM #ColumnList)

BEGIN

SET @ColumnName = (SELECT ColumnName FROM #ColumnList WHERE ID=@counter)

SET @QueryString = @QueryString + '

,' + @ColumnName+ ' = ? '

SET @counter = @counter + 1;

END

SET @QueryString = @QueryString + '

WHERE ID = ?;'

PRINT @QueryString

DROP TABLE #ColumnList

SSIS How to UPDATE instead of INSERT (UPSERT)

I'm working on several SSIS Transfer Packages right now and I just figured out how to do an "UPSERT": How to insert rows that don't exist in the destination table and how to update rows that exist in both source and destination tables. This will work across servers (which is the environment I'm working on).

This is how one table/view would look like in an SSIS package:

Yes, that means (as far as I know) that you have to create four objects for every table you want to transfer data from/to.

The Source object is, of course, connected to the Source database's table/view. The other objects beneath it (the Lookup, Destination and Update objects) are all connected to the Destination database's table/view.

The Update object is the only tricky element in this package. I specify the update script manually, which looks like:
UPDATE dbo.MyTable
SET Column1 = ?
,Column2 = ?
,Column3 = ?
WHERE ID= ?
(see: Generate SSIS Update script which I use to generate the update script for each table that I have in my SSIS package)

Each "?" will be automatically mapped by the system based on the order you defined it. All you have to do next is to specify the Column Mappings. Make sure that you do it in the right order like the image below:


Note how the ID is specified last -- that's because its corresponding "?" was specified last in the Update statement.

How to select value from strongly typed XML

Table Name: TableHasXML
has Column: XML_Column xml(XMLCollectionSchema)

XML data is similar to:
[root xmlns="http://thingswelovetohate.com/schema/2010/PinayProgrammer.xsd" ....]
[row ]
[column1]Things We Love to Hate[/column1]
[column2]http://www.thingswelovetohate.com[/column2]
[/row]
[row]
[column1]Pinay Programmer[/column1]
[column2]http://beingoyen.blogspot.com[/column2]
[/row]
....
[/root]

(yes, I know that was sloppy but blogger wouldn't let me just copy-paste my actual schema. Blogger treats any greater than/less than signs as HTML tags)

XML_Column.query('declare default element namespace "http://thingswelovetohate.com/schema/2010/PinayProgrammer.xsd";
data(/root/row[Column1="Things We Love to Hate"]/Column2)')
FROM TableHasXML

Result = http://www.thingswelovetohate.com

If your XML column or variable doesn't have a namespace then leave out the namespace declaration as well:
XML_Column.query('data(/root/row[Column1="Things We Love to Hate"]/Column2)') FROM TableHasXML

How to know if a string is part of an XML?

I have a table named "TempTable" with an XML column named "XML_Collection". Now I want to know if the string "My Valentine" appears in any of those XML rows.

I used the string manipulator: PATINDEX

SELECT * from TempTable WHERE PATINDEX('%My Valentine%', convert(varchar(max),XML_Collection)) > 0

Important notes:
  • be sure to convert the XML type to varchar(max) first before using any string manipulator functions
  • if PatIndex == 0 then that string pattern does not exist
  • if PatIndex > 0 then that string pattern exists