Archive for April, 2009

SQL Server PowerShell : Basics – Connecting to SQL Server

PowerShell is a great tool that allows DBAs and Developers alike to script, manage and automate SQL Server tasks. Most of the objects and tasks will require SMO (SQL Server Management Objects).

Basic Steps

1. Set Execution Policy

Depending on what you need to do, you may need to set the Execution Policy in your Powershell console. In my case, I will be using it to access SQL Server for administrative purposes, so I will be setting mine to have Unrestricted access to my server. Make sure you review the security guidelines in your organization before you set your environment to Unrestricted

PS> Set-ExecutionPolicy Unrestricted

2. Load assemblies

First, you will need to load SQL Server related assemblies. For SMO, you will usually want to load the following assemblies:

  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SqlEnum
  • Microsoft.SqlServer.SmoEnum
  • Microsoft.SqlServer.ConnectionInfo

When you load the assemblies, you have to use the following format:

[Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, `
      Version=9.0.242.0, Culture=neutral, ` 
      PublicKeyToken=89845dcd8080cc91")

A couple of things to note here:

  • the backtick is the line continuation character, ie if your line of code spans multiple lines, you have to use a backtick
  • you can get the version of the assembly either by :
    • going to the GAC (C:Windowsassembly), right click on assembly and click on properties
    • use reflector

SQL Server SMO assembly

Read the rest of this entry »

VN:F [1.9.22_1171]
Rating: 8.5/10 (26 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)

SQLXML : How to Work With XML Elements (or Nodes) in SQL Server

Assume this is your XML snippet

DECLARE @authorsXML XML
SET @authorsXML = '
<Author>
  <ID>172-32-1176</ID>
  <LastName>White</LastName>
  <FirstName>Johnson</FirstName>
  <Address>
    <Street>10932 Bigge Rd.</Street>
    <City>Menlo Park</City>
    <State>CA</State>
  </Address>
</Author>
'

Note that the examples below show how you can manipulate XML nodes – but most operations require singleton values. Ie, the changes must affect one and only one node. Thus in most the examples we specify the index of the node we want to target.

For example:

(/Author/LastName)[1]

which means we are only targetting the first instance of LastName under the Author node. If you need to do a mass update, you may need to use a cursor.

Read the rest of this entry »

VN:F [1.9.22_1171]
Rating: 9.5/10 (46 votes cast)
VN:F [1.9.22_1171]
Rating: +11 (from 11 votes)

SQLXML : How to Merge Two Nodes Using FOR XML PATH

-- ============================================================
-- sample code that shows how to merge different nodes
-- in the same XML document by using FOR XML PATH()
-- Donabel Santos
-- ============================================================
-- sample XML snippet
DECLARE @sampleXML XML
SET @sampleXML =
'
<Contacts>
 <Contact Type="Main" Value="John Doe">
 <Locations>
 <Location Type="Headquarters" Address="123 XYZ" City="New Westminster"
 Province="BC" Country="CA" PostalCode="V1L1B6" />
 </Locations>
 <Phones>
 <Phone Type="Main" Value="6041112222" />
 <Phone Type="Secondary" Value="6041113333" />
 <Phone Type="Fax" Value="6045553322" />
 </Phones>
 <Emails>
 <Email Type="Main" Value="[email protected]" />
 <Email Type="Secondary" Value="[email protected]" />
 </Emails>
 </Contact>
 <Contact Type="Secondary" Value="Mary Smith">
 <Locations>
 <Location Type="Headquarters" Address="123 ABC" City="New Westminster"
 Province="BC" Country="CA" PostalCode="V1L1B6" />
 </Locations>
 <Phones>
 <Phone Type="Main" Value="6041112255" />
 <Phone Type="Secondary" Value="6041113777" />
 <Phone Type="Fax" Value="6045553311" />
 </Phones>
 <Emails>
 <Email Type="Main" Value="[email protected]" />
 <Email Type="Secondary" Value="[email protected]" />
 </Emails>
 </Contact>
</Contacts>
'
-- get only the elements underneath <Phones> and <Emails>
SELECT
 @sampleXML.query ('(/Contacts/Contact/Phones/*)'),
 @sampleXML.query ('(/Contacts/Contact/Emails/*)')
FOR XML PATH('')
-- result
/*
<Phone Type="Main" Value="6041112222" />
<Phone Type="Secondary" Value="6041113333" />
<Phone Type="Fax" Value="6045553322" />
<Phone Type="Main" Value="6041112255" />
<Phone Type="Secondary" Value="6041113777" />
<Phone Type="Fax" Value="6045553311" />
<Email Type="Main" Value="[email protected]" />
<Email Type="Secondary" Value="[email protected]" />
<Email Type="Main" Value="[email protected]" />
<Email Type="Secondary" Value="[email protected]" />
*/
VN:F [1.9.22_1171]
Rating: 9.8/10 (6 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

I just wrote a couple of SharePoint posts at the Black Ninja Software blog:

How to Programmatically Impersonate Users in SharePoint
– this post shows how you can programmatically execute code in another user’s context, and you can do this by getting a handle to that user’s UserToken

How to Invoke Javascript Snippets Without Using RegisterClientScriptBlock
– this post shows how to invoke and change Javascript code without needing to register that code. This approach uses asp:Literal

And in case you missed this one:
SharePoint Readiness Checklist – Reposted

VN:F [1.9.22_1171]
Rating: 10.0/10 (3 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

SQLXML : How to Use SQL Server XML Function exist()

-- ============================================================
-- sample code on using the SQL Server xml method exist() 
-- several samples shown
-- Donabel Santos
-- ============================================================
DECLARE @xmlSnippet XML
DECLARE @id SMALLINT
DECLARE @value VARCHAR(20)
SET @xmlSnippet = 
'
<ninjaElement id="1">SQL Server Ninja</ninjaElement>
<ninjaElement id="2">SharePoint Ninja</ninjaElement>
<ninjaElement id="3">ASP.NET Ninja</ninjaElement>
'
-- this is what we will look for
SET @id    = 2
SET @value ='SQL Server Ninja'
-- note exist() will return only either :
-- 1 (true) or 0 (false)
-- check if a node called ninjaElement exists
-- at any level in the XML snippet
SELECT @xml.exist('//ninjaElement')
-- check if a node called bar exists
SELECT @xml.exist('//bar')
-- check if attribute id exists anywhere
SELECT @xml.exist('//@id')
-- check if attribute id exists within a ninjaElement tag
SELECT @xml.exist('//ninjaElement[@id]')
-- check if the id attribute equals to what we saved 
-- in the @id variable
SELECT @xml.exist('/ninjaElement[@id=sql:variable("@id")]')
-- check if the node text equals to what 
-- we saved in the @value variable
SELECT @xml.exist('/ninjaElement[text()=sql:variable("@value")]')
VN:F [1.9.22_1171]
Rating: 7.5/10 (41 votes cast)
VN:F [1.9.22_1171]
Rating: +7 (from 11 votes)

SQLXML : How to Join Multiple XML Snippets (using query() and UNION ALL)

-- ============================================================
-- sample code on joining multiple XML snippets 
-- using query() and UNION ALL
-- Donabel Santos
-- ============================================================
-- declare xml variables
DECLARE @firstXMLSnippet XML
DECLARE @secondXMLSnippet XML
-- first XML snippet
SET  @firstXMLSnippet = 
'
  <attributes>
    <attribute id="13" name="EmpNo">
      <item value="10" />
    </attribute>
    <attribute id="44" name="Position">
      <item value="Manager" />
    </attribute>
    <attribute id="32" name="Address">
      <item value="123 XYZ St." />
    </attribute>
    <attribute id="33" name="City">
      <item value="Vancouver" />
    </attribute>
  </attributes>
'
-- second XML snippet
SET  @secondXMLSnippet = 
'
    <attribute id="37" name="Province">
      <item value="BC" />
    </attribute>
    <attribute id="52" name="Comment">
      <item value="SQL Server XML How To" />
    </attribute>
'
-- use query() to extract just the <attribute> elements
-- join using UNION ALL
-- add the root element back by using ROOT() option
SELECT  @firstXMLSnippet.query('//attribute')
UNION ALL
SELECT  @secondXMLSnippet
FOR XML PATH (''), ROOT('attributes'), TYPE
--result
/*
<attributes>
  <attribute id="13" name="EmpNo">
    <item value="10" />
  </attribute>
  <attribute id="44" name="Position">
    <item value="Manager" />
  </attribute>
  <attribute id="32" name="Address">
    <item value="123 XYZ St." />
  </attribute>
  <attribute id="33" name="City">
    <item value="Vancouver" />
  </attribute>
  <attribute id="37" name="Province">
    <item value="BC" />
  </attribute>
  <attribute id="52" name="Comment">
    <item value="SQL Server XML How To" />
  </attribute>
</attributes>
*/
VN:F [1.9.22_1171]
Rating: 9.6/10 (15 votes cast)
VN:F [1.9.22_1171]
Rating: +4 (from 4 votes)

Black Ninja Software is looking for another Ninja to join the team.

Shereen has posted the “requirements” – and that really sums up to a few words.

Passion and love for what you’re doing.

We do a lot of SharePoint/SQL Server/ASP.NET, but we’re not necessarily looking for the technical skills. Don’t get me wrong, that is very important. You have to be technically adept. However, we also recognize that the technical skill by itself is a hollow measure of what makes a good addition to the team. The passion and the love for what you’re doing will transform into l33t technical skills, just give it some time.

I used to be a PHP/Perl/bash shell/a-little-bit-of-Python developer. When I first applied to a Microsoft-centric company, I had no qualms saying I didn’t code in C# nor ASP.NET, but that I loved learning, and that I loved to program and to do databases. And that wasn’t lip service. As you get more experience, you also realize that although you’d love to learn all cool languages, you pick a few favorite ones and that’s what you master. I still love programming in PHP, but my passions right now are SQL Server (top spot, no question, no competition), SharePoint and C#/ASP.NET. Throw in a whole lotta CSS and a handful of design/creative work :)

trephine commented on the Black Ninja blog (excerpt):

:
100% agree on the list of positive and negative indicators. When I had an opportunity to interview potential engineers, the one question I got the most out of was “If you had enough money that you didn’t have to work, what would you do with your days?”
:

I thought that was a very interesting question. What would you do?
(I posted my comment at the Black Ninja Blog – Looking for another Ninja – check it out and we’d love to hear what you’d do)

Or if you’re interested to become a (cool) Ninja, send us your Ninja-file ([email protected]).

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
`