Wednesday, August 22, 2007

Correcting int identity values after a sql server 2005 to 2000 publish

You will need to run this sql:


DECLARE @TableName varchar(50), @ColumnName varchar(50)
DECLARE @SQLString nvarchar(500), @MaxID int

DECLARE curs CURSOR FOR
--begin query
select TABLE_NAME, COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
-- end query
OPEN curs
FETCH NEXT FROM curs INTO @TableName, @ColumnName
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2 )
BEGIN
SET @SQLString = N'SELECT @MaxIDOUT = MAX( ' + @ColumnName + ') FROM ' + @TableName
EXECUTE sp_executesql @SQLString, N'@MaxIDOUT int OUTPUT', @MaxIDOUT=@MaxID OUTPUT;
DBCC CHECKIDENT(@TableName, 'RESEED', @MaxID)
END
FETCH NEXT FROM curs INTO @TableName, @ColumnName
END
DEALLOCATE curs

Friday, March 30, 2007

Finding your left and right neighboring rows using a temp table


/* declare variables */
DECLARE @userID INT
DECLARE @maxCnt INT
DECLARE @tmpUser TABLE (tmpId INT IDENTITY(0,1), UserID INT)

/* quick way to limit to one user */
SET @userID = 223

/* populate the temp table */
INSERT INTO @tmpUser (UserId)
SELECT UserId FROM users

/* find max value for modulo math */
SELECT @maxCnt = COUNT(userid) FROM @tmpUser

--select * from @tmpUser

/* join against orginal data, using mod math and tmp table */

SELECT u.UserId, l.UserId, r.UserId
FROM users AS us
left join @tmpUser AS u ON u.userid = us.userid
left join @tmpUser AS l ON ((u.tmpId - 1 + @maxCnt) % @maxCnt) = l.tmpId
left join @tmpUser AS r ON ((u.tmpId + 1)% @maxCnt) = r.tmpId
WHERE us.userID = @UserID

Friday, February 02, 2007

MS SQL Cursors Example

This is an example of how to drop a number of Tables in SQL server using a cursor:

DECLARE @name varchar(255)
DECLARE curs CURSOR FOR SELECT name
FROM sysobjects
WHERE name LIKE 'tmp_%'
AND DATEDIFF(day,crdate,getdate())>2
AND type = 'U'
OPEN curs
FETCH NEXT FROM curs INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2 )
BEGIN
PRINT 'DROPPING TABLE ' + @name
EXEC('DROP TABLE ' + @name )
END
FETCH NEXT FROM curs INTO @name
END
DEALLOCATE curs

Monday, December 11, 2006

Cleaning Input in ColdFusion

I have to do some work with ColdFusion from time to time. Here an example of how to clear the input for a list of integers to be passed to a database:
<cfparam name="category_id" default="">
<cfoutput>category_id: #category_id#</cfoutput>
<cfset clean_cat=" arrayNew(1)">
<cfloop index="ListElement" list="#category_id#">
<cfif>
<cfset x=" ArrayAppend(clean_cat,">
</cfset>
</cfif>
</cfloop>
<!--- clean out here --->
<cfoutput>clean_cat: #ArrayToList(clean_cat, ',')#</cfoutput>

Sunday, February 20, 2005


I like the closet space. Posted by Hello

Ah, my pretty bike. Posted by Hello

This picture is looking out of my windows. Posted by Hello

The bed in my new room. Check out the "Endless Summer" picture. I like the match! Posted by Hello

Friday, February 18, 2005

Fun with the phones.

So I some how turned on the hold music for the company so that it would play out of my phone. I had no idea that this was the case, but randomly this morning, my phone made the sound of a rebooting computer. I jumped up and asked, "Did any of you hear that?" Everyone kind of stared at me as if I had three heads. So I went to Seth, my boss, and asked him if he had rebooted his computer. He said, "yeah, why?" After bantering back and forth for a while, we came to the realization that the hold music for the company was playing out of my phone. Apparently, the hold music for Miller Systems is supplied from Music Match Juke Box on Seth Miller's backup computer. He had just rebooted this computer and that was the sound that I heard. This was not previously a problem because hold music coming out of his computer is a new feature that was just setup within the past few days. He told me how to turn this feature off on my phone and sent me on my way. By this point, I came back to my cubicle to hear Funk music pumping out of my phone. I pressed the designated key sequence to turn it off...and the music kept right on coming. So I went to Ben's phone, and pressed "Feature 86". Ben's phone started to sing to us. Maria, my manager, said, "hey, it doesn't work on my phone!" so I ran over there and pressed the magic sequence on her phone, turned up the volume, and within 30 seconds, the whole office was rocking out to Seth's magic Funk tunes. "Hmmm, I wonder how you /really/ turn this off," someone asked. So I trundled back to Seth's office to ask how to /really/ turn off the music. He tried punching Feature 86 on his phone and it started to sing to him. He punched it in again...still singing, again...still singing. Finally, in his infinite wisdom, he tried the novel Feature #86 command and, low and behold, the music ceased as quick as it began. I ran back to my cubicle and spread the good word. Now we know how to both turn the music *on* and *off*.

Tuesday, February 01, 2005

Everything under the sun

Yesterday, I hung out with a a really wonderful friend from high school, Lisette. She called me during the day but I couldn't take her call then, so I finally caught up with her around 6:15pm. I hoofed it from 3rd and 56th street uptown to 10+ blocks to her appt. and gave her a call so she would buzz me in. We then went out to a Japanese restaurant for dinner, a nice little place about 3 blocks away. We dined and talked for a few hours, she offered up three choices: a) we could head out to a bar and catch a drink, b) we could walk to a neo-bohemian coffe shop that she often frequents, of c) we could head back to her place and catch some tea. I really like Lisette's studio appt., but I coffee sounded really good to me to I elected to go with plan B. Another 10-12 blocks later, and after Lisette doubted herself about which block the place was on (she claimed that this is a frequent occurs, ah how many times does a place "move" on me...only all the time...;), we made it! I order a mocha, she a latte, and we sat down in the back and change for another three hours. We talked about everything from politics, to friends, to work, to the weather, to you name it. At around 11:00pm, we were both starting to turn into pumpkins so we decided to call it a night. I came back up to her place to pick up my back, talk for a bit more, and then grabbed a cab back to the Kimberbly Hotel on 50th where I was staying.

Lisette is a really good thinker and a really good listener which makes for delightful conversations. We're both knowledgable enough about a broad range of topics that we can talk about nearly anything despite the fact that she's been working in performing arts for the past two years since she graduated and I have been working in computer software. On the matter of geography, we're each a little bit right and a little bit wrong.

We had a discussion about the temperature difference between Boston and New York City. I claimed that NYC is mainly west of Boston, with only a little bit of north/south change. She claimed that NYC was far east of the western border of Massachusetts and that most of the trip from Boston to NYC was spend traveling north/south. She said this is why NYC is halfway between Boston and Washington, DC. and also why it is so much warming in NYC than in Boston. Well we were each a little bit right. She was right about the temperature difference and that NYC is far south of Boston. I was right that New York City is aligned with the West Coast of Massachusetts. ;)