<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: tsql code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 07 Aug 2008 17:07:58 GMT</pubDate>
    <description>DZone Snippets: tsql code</description>
    <item>
      <title>PRNG In SQL Select </title>
      <link>http://snippets.dzone.com/posts/show/4033</link>
      <description>Simple example of a PRNG (pseudo-random number generator) written into a SQL statement&lt;br /&gt;&lt;br /&gt;Example is in T-SQL, but it ports well&lt;br /&gt;&lt;br /&gt;Actual application should use either a better random algorithm, or the output be used with randomized seeds. This is definitely not cryptographically secure. It's very handy if you need a simple random number with your recordset though.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- Setup some vars we'll need&lt;br /&gt;DECLARE @prng TABLE (seed BIGINT, rnum nchar(10))&lt;br /&gt;DECLARE @seeds TABLE (seed BIGINT)&lt;br /&gt;DECLARE @seed BIGINT&lt;br /&gt;DECLARE @C1 BIGINT, @C2 BIGINT, @C3 BIGINT&lt;br /&gt;SET @seed = 0&lt;br /&gt;SET @C1 = 1664525&lt;br /&gt;SET @C2 = 4294967296&lt;br /&gt;SET @C3 = 1013904223&lt;br /&gt;&lt;br /&gt;-- Create a seed table so we can have some data to use&lt;br /&gt;WHILE @seed &lt; 10&lt;br /&gt;BEGIN&lt;br /&gt;    INSERT INTO @seeds (seed) VALUES (@seed)&lt;br /&gt;    SET @seed = @seed + 1&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;-- Create our PRNG (inserts into table for illustrative purposes)&lt;br /&gt;-- prng(seed) ::= ((((C1 * seed) % C2) + C3) % C2) / C2&lt;br /&gt;-- Then convert prng(seed) into a string&lt;br /&gt;-- of 10 chars, 8 of which are decimal places&lt;br /&gt;INSERT INTO @prng&lt;br /&gt;SELECT&lt;br /&gt;    seed,&lt;br /&gt;    REPLACE(&lt;br /&gt;        STR(&lt;br /&gt;            ( CAST((((@C1*seed)%@C2)+@C3)%@C2 AS FLOAT) )&lt;br /&gt;            / ( CAST(@C2 AS FLOAT)),&lt;br /&gt;            10, 8&lt;br /&gt;        ),&lt;br /&gt;        ' ', '0') AS rnum&lt;br /&gt;FROM @seeds&lt;br /&gt;&lt;br /&gt;-- Let's take a look at what we created&lt;br /&gt;SELECT * FROM @prng&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:39:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4033</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>TSQL overwrite whole table column with the value of one row in the column</title>
      <link>http://snippets.dzone.com/posts/show/1853</link>
      <description>You want to remove some sensitive data from a column in a table in a test database. Just to make things hard the secret data within the column are massive encoded blobs. Large blobs dont work in subqueries nor the standard datbase tools - you would have to extracting it to disk. So you are looking for an 'update from select' query do solve the problem. &lt;br /&gt;&lt;br /&gt;You have added one new row into the table that has test data in the sensitive column. You now want a query to write the sensitive column in this new row over the rest of the column in the table. The following code does the job: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;update SECRET_TABLE set SECRET_COLUMN = SOURCE_TABLE.NOT_SECRET from SECRET_TABLE st1 inner join (&lt;br /&gt;select st2.[ID] as [ignore], st3.[ID], st2.[SECRET_COLUMN] as [NOT_SECRET] from SECRET_TABLE st2 cross join SECRET_TABLE st3 where st2.[ID] = 'not_secret_row_id'&lt;br /&gt;) as SOURCE_TABLE on SOURCE_TABLE.[ID] = st1.[ID]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;it is a bit slow due to the cross join but as it was an infrequent task it was not a problem for me. any hints on doing it without the cross join? </description>
      <pubDate>Thu, 06 Apr 2006 20:12:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1853</guid>
      <author>simbo1905 ()</author>
    </item>
    <item>
      <title>DBMAIL</title>
      <link>http://snippets.dzone.com/posts/show/1762</link>
      <description>// sql server 2005 sample dbmail send&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;EXEC msdb.dbo.sp_send_dbmail @profile_name = 'dbmailprofile', @recipients = 'me@co.com,you@cocom', @body = 'the body', @subject = 'the subject'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 18:39:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1762</guid>
      <author>codeguy (Sean Harvell)</author>
    </item>
    <item>
      <title>Simple Raise Error</title>
      <link>http://snippets.dzone.com/posts/show/1761</link>
      <description>// simple raise error&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;IF UPDATE(lastname)&lt;br /&gt;BEGIN&lt;br /&gt;	RAISERROR ('cannot change lastname (source = instead of)', 16, 1)&lt;br /&gt;	ROLLBACK TRAN&lt;br /&gt;	RETURN&lt;br /&gt;END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 18:26:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1761</guid>
      <author>codeguy (Sean Harvell)</author>
    </item>
    <item>
      <title>SQL Server 2000 RegEx</title>
      <link>http://snippets.dzone.com/posts/show/1760</link>
      <description>// SQL 2K RegEx Compat Code&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;CREATE FUNCTION dbo.find_regular_expression&lt;br /&gt;	(&lt;br /&gt;		@source varchar(5000),&lt;br /&gt;		@regexp varchar(1000),&lt;br /&gt;		@ignorecase bit = 0&lt;br /&gt;	)&lt;br /&gt;RETURNS bit&lt;br /&gt;AS&lt;br /&gt;	BEGIN&lt;br /&gt;		DECLARE @hr integer&lt;br /&gt;		DECLARE @objRegExp integer&lt;br /&gt;		DECLARE @objMatches integer&lt;br /&gt;		DECLARE @objMatch integer&lt;br /&gt;		DECLARE @count integer&lt;br /&gt;		DECLARE @results bit&lt;br /&gt;		&lt;br /&gt;		EXEC @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;		EXEC @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;		EXEC @hr = sp_OASetProperty @objRegExp, 'Global', false&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;		EXEC @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignorecase&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;			&lt;br /&gt;		EXEC @hr = sp_OAMethod @objRegExp, 'Test', @results OUTPUT, @source&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;		EXEC @hr = sp_OADestroy @objRegExp&lt;br /&gt;		IF @hr &lt;&gt; 0 BEGIN&lt;br /&gt;			SET @results = 0&lt;br /&gt;			RETURN @results&lt;br /&gt;		END&lt;br /&gt;	RETURN @results&lt;br /&gt;	END&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 25 Mar 2006 17:09:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1760</guid>
      <author>codeguy (Sean Harvell)</author>
    </item>
  </channel>
</rss>
