joeydaly.com … tech blog & sometimes more

11Dec/120

SQL – How to get a row of results into a comma delimited list using FOR XML PATH and STUFF

Posted by Joey

Okay had this problem come up at work recently and it’s fantastic solution. It’s a bit hard to explain when you would need it, I guess it works well for when you’re doing table joins and need quick reference data out within a query.

Anyway here’s how you do it.

Firstly I’ve got a table ‘Test’ that contains two columns ID & Name. I’ve added some data in… Continue reading

3Dec/120

How to do REPLACE string UPDATES in MySQL

Posted by Joey

So recently I updated some sql code for my blog, anyway I thought I’d share so here it is.

1

Basically go and look for every blog post where it had the old url in the content and replace it with the new url. Best to try it out on one link before you go and… Continue reading

23Nov/110

How to check for active transactions

Posted by Joey

Debugging an issue today at work, and needed to find if there was any uncommitted transactions or active transactions.

There’s a couple of ways to do this:

1
Thanks Pinal Dave – more from his blog post here.

Also you can use dbcc opentran Or SELECT @@TRANSCOUNT

So, if you’re like me and what to see this in action… Continue reading

1Jul/110

How to find the name of a sql server

Posted by Joey

Fast way to get a sql server name, great if you’re dealing with multiple sql servers.

1

This will return the Windows computer name on which the sql server instance is currently running on.
It will return NULL if there is an error.

1

This will return the instance & computer name

[code… Continue reading

16May/110

how to select columns from a stored proc

Posted by Joey

Okay so recently I’ve had to select only certain columns from a large stored proc that may return like 4 columns. How to do this?

DECLARE @tableVar table (Surname varchar (100), FirstName varchar(255), Email varchar(100), Age int)
INSERT INTO @tableVar EXEC dbo.usp_getSomeData
SELECT Surname, Email FROM @tableVar… Continue reading

19Apr/110

Passing a nullable/blank field to a WHERE statement

Posted by Joey

I’ve used this piece of code in many places lately to check if a field is nullable or blank – used mainly as a default value.

AND
(
@ResultType = ” OR
@ResultType ” AND ResultType IN(@ResultType)
)… Continue reading

5Apr/110

grant execute for a stored proc to application role

Posted by Joey

simple script incase you forget:

grant exec on to… Continue reading

25Nov/100

Using DELETE FROM with INNER JOIN

Posted by Joey

Ever needed to delete a table? Oh wait it’s joined to another. Well here’s some how you do it… the right way!

For MSSQL

DELETE Users
FROM Users u
INNER JOIN
Profiles p on u.UserID = p.UserID
WHERE u.UserIsArchived = 1

For MySQL

DELETE u, p
FROM Users u
INNER JOIN Profiles p ON u.UserID = p.UserID
WHERE u.UserIsArchived = 1… Continue reading

12Nov/100

Example script for create table in h2

Posted by Joey

Just figured I’d add one of these scripts incase I forget! Haha… but just if you’re curious, it not different to MySQL.

DROP TABLE IF EXISTS Players;
CREATE TABLE Players
(
PlayerID int identity,
FirstName varchar(255),
LastName varchar(255)
);

INSERT INTO Players
(FirstName, LastName)
VALUES
(‘Joey’, ‘Daly’);

Here’s the docs for H2 DB:

12Nov/100

Fusegrid SDK

Posted by Joey

Been using Fusegrid SDK, it’s fantastic for people who just want to start coding Coldfusion with zero time wasted on setup.

Normally you’ll need to setup a web server, then download ColdFusion and then work out what database you want… painful!

Fusegrid comes bundled with Railo + H2 Database, and CFWheels :D

Get it here: http://www.fusegrid.com/sdk/… Continue reading