So this is freaking great!!! I finally learned how to manipulate data by pulling bits and pieces out of a sql statement to use in other SQL statements. Please note that I have not tried this in SQL Server 2000 but it sure works great in SQL Server 2005 and up.

Let’s say that you want to add a column to a table and then populate it with data that is in the table already. (I know redundant data, however the thought at work is that it is primer data that the end user can change as opposed to a locked value that they can do nothing with.)

This script is going to be used in an upgrade to an existing application. So if the change has already happened we want to by pass this in lieu of the current data that is in the system.

First you want to check to see of your field exists and if it does not add it.

IF not exists (select * from sys.columns where [object_id] = (
Select [object_id] from sys.tables st JOIN sys.schemas ss ON st.schema_id = ss.schema_id where st.name = 'table_name')
AND name = 'new_field_name')
BEGIN
ALTER TABLE [dbo].[table_name] ADD [new_field_name] [char](12) NULL
END
GO

Because my in house example will create temporary tables I want to check if the temp tables exist and if so drop them

IF  EXISTS (SELECT name FROM tempdb..sysobjects WHERE name like '#Temp%')
drop table #Temp
GO
IF  EXISTS (SELECT name FROM tempdb..sysobjects WHERE name like '#Temp2%')
drop table #Temp2
GO

Note the ‘%’ in my statements above. It is necessary because Microsoft adds a lot of junk to the end of the temp table.

I know the following can go several different ways. So, I am giving you the quick and dirty how to. The next thing I did was set a declaration that will be used to pull the count total out for use later in the conditional logic.

Declare @t_intTotal Int
SELECT @t_intTotal=count(unique_field_name) FROM table_name WHERE new_field_name is not null
print @t_intTotal

With normal queries we are able to set the count(unique_field_name) as intCountFieldName and use the as variable from our recordset. Not so from within SQL server. To use the data from count(unique_field_name) I had to set @t_intTotal = count(unique_field_name). This makes it available later on in the process.

Next we have to declare some more variables that will be used if all of our new field_name fields are null. (Again, think quick and dirty. Feel free to clean it up if you like.)

Declare @Count Int
Declare @LoopCount Int
Declare @populate_value char(3)
Declare @t_populate_value char(3)

Using the new @t_intTotal variable we are able to write a simple if statement and run some code if it is necessary. After the first check we have our first of 2 temp tables. The data from here will be used to create the second temp table with an identity added to it.

IF @t_intTotal = 0
Select unique_field_name -- also the seed data value
into   #Temp
from  table_name

Using the first temp table we add the identity to a second temp table. This gives us a unique key that we can loop over when trying to add data to the table that already exists. We will set the Count to the total rows from the new query and set loop count to start at 1. Note if you pull an identity field in as one of your variables you cannot do the Identity(int,1,1) id. Only one identity field per table.

IF @t_intTotal = 0
Select Identity(int,1,1) ID, unique_field_name into #Temp2 from #Temp
Set @Count = @@RowCount
Set @LoopCount = 1

Using the second temp table we are able to loop over the recordset making an update with each iteration. In my case it is important to note that unique_field_name is a unique record so that we can use it to make sure the right seed data is going into the correct column.

IF @t_intTotal = 0
While @LoopCount <= @Count
Begin
Select @t_populatevalue=unique_field_name  from #Temp2 Where ID = @LoopCount
Set @LoopCount = @LoopCount + 1
update table_name
set new_field_name = @t_populate_value
where unique_field_name = @t_populate_value
End
GO

Note that I had to add unique_field_name data as the seed data in the new_field_name column.

This may be old hat for some, but when searching the web it is near impossible to find all of this in one place. I hope that you enjoy it and can mod it for your use. Let me know what you think.

So, I was debugging some merge and replication issues for SQL Server 2005 yesterday with a co-worker and we realized that we pointed the snapshot to the wrong directory. Everything else was created properly and the only thing that wasn’t working was the snapshot generator.

When one generates the scripting for creating a publication there are stored procedures that assign the snapshot directory to the distribution.

sp_updateextendedproperty
sp_addextendedproperty

I thought, sure that is what I will use to get it working in the correct directory. All I have to do is use the sp_updateextendedproperty and reset the path. Well, that didn’t work. May work for others but didn’t work for me.

So I had to get a little more drastic. I went digging. One of the nice things about SQL server is that everything is handled and configured in the server. After digging for a while, I found the following:

In the distribution database there is a stored procedure called sp_MSadd_snapshot_agent. It points me to the msdb database. Run this query and you will see the snapshot directory.

SELECT * FROM msdb..MSdistpublishers

Now that I know where it is at, a simple update script can change it.

update msdb..MSdistpublishers
set working_directory = ‘\\your new snapshot directory\’
where name = ‘name of your publication’

That’s it. Subscribers can now be added and all will be well.

Another, quick gotcha you may run into with this involves permissions on the snapshot directory. If they are not configured properly then the snapshot will still not get generated.

Posted by: tszao | June 12, 2008

Ruby on rails 2.0 blog in 15 minutes

I was searching for a fix to some issues I had with text_field and forms and found this.

http://skionrails.wordpress.com/tutorials/how-to-write-a-blog-in-15-minutes/

It not only helped with my issue, it also updates the now famous DHH video for pre-2.0 rails.

It is very detailed and useful if you are getting started with Rails 2.0

Posted by: tszao | June 11, 2008

rails 2 find_by and find differences

So this has been a day of bug tracking.

I use a very simple authentication process for a site that I am writing in Rails 2.0. Here is the def in my model.

#Original def
def self.authenticate(user_info)
user = find_by_username(user_info[:username])
if user.username == user_info[:username] && user.hashed_password == hashed(user_info[:password])
return user
end
end

This may look familiar to some out there if you read the same materials that I read for pre-Rails 2 literature. While trying to get all of my login stuff working with Rails 2 this creates a …

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.username

This was a royal pain in the butt to fix but here is a work-around. I am not sure how viable it is as a solution, but I do know that it fixed my problem and now I can move on to the rest of the site without this thing hanging over my head.

Apparently in Rails 2.0 the way functions are called work differently than in Rails 1. I needed to use a different query to accomplish the same task. However, this particular query returns an array. So after you check the length of the recordset ( no [0] ) then you have to use the array ( with [0] ) to work with the results. Crazy thing is that when the results are returned to the controller, the need for the array disappears.

#work around def
def self.authenticate(user_info)
#get user using find instead of find_by_username
user = User.find(:all, :conditions => ["username = ?", user_info[:username]] )
#to check length of object you don’t need [0]
if user.length > 0
#to use the object you do need the [0]
if user[0].username == user_info[:username] && user[0].hashed_password == hashed(user_info[:password])
#when the result gets back to the controller the [0] will not need to be used
return user[0]
end
end
end

Let me know if you have a better solution for the work around.

Posted by: tszao | June 11, 2008

Merge and Replication Fake Out

So I have been trying to work with merge and replication lately. (SQL Server 2005) I am still by no means an expert, however, I ran into another issue that I would like to share how it was worked around.

We have a development box that we used at a conference to demonstrate “our” merge and replication capabilities. Brought it back to the office and started to have issues with it. Essentially the distributor was already set up on the box but nothing was connected to it and we could not get rid of it.

I was assured by our network admin that the box had been cleaned. I have my doubts though. Finally, I got tired of trying to work with the muxed up database and created a fresh named instance.

Once we got it set up in the Surface Area Configuration Manager, all worked smoothly. We were able to set up a Publication with the same box as the distributor. Today we will add subscriptions.

I guess that I just needed to vent because training someone to do what you know can be a pain sometimes. Not necessarily because of the person, but just because the technology doesn’t always function properly.

I don’t know how tech support people do it.

So you are new to Visual FoxPro 9. So am I. A little while ago I put out a cry for help to the community and got a couple of great responses from Kevin and Tod who were instrumental with helping me get my bearings.

Since the FoxPro Hell post I have managed to gather a few resources that have been very helpful with getting me up and running. Yes, I have progressed from FoxPro Hell to FoxPro Purgatory.

I am finally starting to get my head wrapped around the code base and there is actually light at the end of the tunnel. Let’s just hope it isn’t an oncoming train.

If you are new to FoxPro or Visual FoxPro 9, here is a list of resources that I have found very helpful.

Free (that’s right FREE) FoxPro Tutorial Videos
Garfield Hudson – Learn Visual FoxPro – Free!!!
Sweet Potato Software – Learning Visual FoxPro (Scroll to the bottom of the blog post)

Blogs – From people in the FoxPro Community
http://kevinragsdale.blogspot.com/
http://blog.todmeansfox.com/
http://www.sweetpotatosoftware.com/SPSBlog/
http://learningvfp.blogspot.com/
http://doughennig.blogspot.com/
http://weblogs.foxite.com/bernardbout/
http://blogs.msdn.com/calvin_hsia/default.aspx
http://cathypountney.blogspot.com/
http://weblogs.foxite.com/vfpimaging/
http://spacefold.com/articles.aspx
http://talkingfox.blogspot.com/
http://weblogs.foxite.com/emersonreed/
http://www.rickborup.com/blog/
http://www.rickschummer.com/blog/
http://www.geeksandgurus.com/blogs/sjb/
http://weblogs.foxite.com/stuartdunkeld/
http://blog.visionpace.com/

Conferences
Southwest Fox

Other Useful Links
http://www.foxite.com/
http://www.universalthread.com/
http://fox.wikis.com/wc.dll?Wiki~VisualFoxProWiki
http://www.cs.trinity.edu/~thicks/Tutorials/ (scroll down to the FoxPro tutorials)
http://learningvfp.blogspot.com/2005/12/learning-foxpro-articles.html#links

Posted by: tszao | May 12, 2008

ActionMailer set-up differences

Ok, here is one more thing for those who are used to the pre Rails 2.0 days.

When setting up the ActionMailer in your environment.rb, you will need to make a slight adjustment with the syntax.

Previously the code looked like this:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => “mail.langorang.com”,
:port => 25,
:domain => “langorang.com”,
:authentication => :login,
:user_name => “no-reply@langorang.com”,
:password => “**************”,
}

Now it looks like this (notice the bold)

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings= {
:address => “mail.envion.com”,
:port => 25,
:domain => “envion.com”,
:authentication => :login,
:user_name => no-reply@envion.com”,
:password => “*********”,
}

server_settings need to be changed to smtp_settings. I know it is minor but it will give ya real fits if you are trying to install simple captcha in a rails 2.0 environment.

If you are getting the following error using Instant Rails for Rails 2.0 here is a quick fix that will get you working again.

“The procedure entry point mysql_stmt_row_tell could not be located in the dynamic link library libmysql.dll”

You will need to manually copy the libmysql.dll to the \ruby\bin directory. You can find the libmysql.dll in the \mysql\bin directory in side you Instant Rails folder.

Posted by: tszao | May 1, 2008

I died and went to FoxPro Hell

I am in FoxPro Hell!!!

At my day job, we have a project that was written in FoxPro and eventually bumped up to Visual FoxPro 9. It works. The project actually fires up and still does what it is supposed to do. However, I have been tasked with trying to clean it up. I have never worked with FoxPro before and it is ugly. At least this codebase is ugly. Some things make sense and others do not.

Currently I am trying to figure out how to release a Page Frame tab from a parent class. I get the following error.

“Cannot delete objects because some are members of a parent class”

Frustrating, because I have removed all the code that makes the thing work and I still get this error. If anyone out there has any ideas, I would be eternally grateful.

I got this error recently and wanted to share with the world what took me a day to debug.

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

This will look like a total duh moment but it makes sense when one considers the circumstances.

The first response (assuming you know the SQL instance is running) to above is to go to the Surface Area Configuration manager for SQL 2005. Once there, make sure that your instance is set up to work with TCP and Named Pipes.

The next course of action is to check the firewall to see if it is blocking the server or the ports you need to access the system remotely.

If none of that is an issue, you need to consider another option. Look in your services. I am going to assume that the sql server instance is started and you have at least tried to connect to it from you local SSMS.

However, what about the SQL Server Browser service? If you are like me, you want to keep as little from starting at start-up as possible and shut down services when they are not used frequently. However, if when you start the server instance and forget to start the SQL Server Browser instance you will run into an error very similar to the one above. Make sure that your SQL Server Browser service is started and you should be good to go.

Older Posts »

Categories