Currently showing entries with the tag: shortcut

page 1 of 1
1 

Writing SQL Scripts to Generate SQL Scripts

September 01, 2007 • 6:43AM • permalink
This came up at work the other day, but it's a very helpful trick if you find yourself replicating the same set of tables over and over again.

Let's say you have the following table:


CREATE TABLE [countries]
(
   country_id   INT,
   country   NVARCHAR(64)
);



Let's say I use a common list of countries everytime I create a new website. The above data is prepopulated and we'll say its filled with about 200 rows. While there are a few easy ways to generate the script, it seems like a waste to do something like create an entire C# program or even a write Python script.

Fortunately, you can use the fact that SELECT can return static data to quickly generate the script, like so:


SELECT
   'INSERT INTO [countries]
      (country_id, country)
   VALUES
      (' + CAST (country_id AS VARCHAR(3)) + ', ''' + country + ''');'
FROM [countries];
--Note the use of the '' to escape the single-quote for output



This would return the following:

INSERT INTO countries (country_id, country) VALUES (1, 'United States');
INSERT INTO country (country_id, country) VALUES (2, 'Afghanistan');
INSERT INTO country (country_id, country) VALUES (3, 'Albania');
...


All you're doing is SELECTing out a concatenated string that will generate your commands. Then you can copy and paste it to a text file, which you can use as a reusable utility script.

This can be very helpful when you use a common framework that gets replicated for several projects. I personally use it for doing things like the above country example, prepopulating geographical information, adding default administrator accounts to new websites, and propagating a list of banned IPs.


HTTP Status Code 307 - Temporary Redirect

August 19, 2007 • 9:41AM • permalink
I'm sure that many of you haven't gotten past the title before saying, "No, a HTTP Status Code of 302 is the Temporary Redirect." I'd like to briefly explain the difference between the two and show you how you can benefit from a 307 redirect. Please also note, that this is a temporary redirect and should probably be avoided in most production situations. You will see one case in particular below where this can be useful.

In .NET, the standard way to redirect between pages is to use the Response.Redirect() method. This implicitly flushes the Response buffer and instead of sending back HTML (with a status code 200 OK), it sends the user a 302 Found code, meaning the requested page was found, but under a different URI. The server also sends back the new URI in the Location header that should be subsequently retrieved by the client.

This works in most cases, except for one minor problem: the case when you're trying to POST data to the server. There are many ways around this problem (including changing it to a GET/QueryString combo), but if a POST is necessary a 307 Temporary Redirect status code will indicate to the browser that the POST method should be retained.

This can be very valuable when developing on a machine with Windows XP Pro (and hence IIS 5.1 which doesn't allow you to identify web sites with host headers.) Under IIS, I setup multiple projects with the directory schema C:\Inetpub\wwwroot\ProjectName\. In my code, I prefix all links with an Web.config driven value that indicates the subdirectory off the root that the project resides in. This way, I can use the value /ProjectName/ in dev and / in production and the links will work in both environments. I don't like to pass these values around though, so when I recently wrote a small Flash application specifically for one website, I wanted to hardcode the SWF to POST to 'http://www.domainname.com/'. It seemed like a lot of work at first, but with a 307 redirect, it was simple!

If the Flash file was hardcoded to POST to 'http://www.domainname.com/PageName.aspx', you just create a file in the localhost root directory (C:\Inetpub\wwwroot\ in my example) called PageName.aspx and run the following on Page_Load():


Response.StatusCode = 307;
Response.RedirectLocation = "/ProjectName/PageName.aspx";



This will allow proper testing in IIS 5.1 and will redirect to the correct page with a POST method and all associated form data.

One final note as to why this should only be used for testing. Part of the definition of a 307 status code directs that browsers should prompt the user of the redirect and allow them to replicate the action (for older browsers). Firefox, in particular, is one browser that fully supports the standard and prompts the user, asking if they want to allow the form data to be sent via a POST. Since this could lead to a very poor user experience, I would recommend limiting use of the 307 status code to development and testing environments only.


How to Bring Up Task Manager During a Remote Desktop Session

August 08, 2007 • 9:38PM • permalink
This was something I'd wondered how to do for a long time. In Windows, all images (or exe files) that are started are created as children of the Winlogin.exe process, which is created when a user first logs in. As a [intentional] consequence of this, Winlogin.exe captures the Ctrl-Alt-Delete control key sequence which can't be overridden by another process.

Sometimes when connecting to another computer over the network, I might need to use Task Manager to suddenly terminate an application in order to free up either memory or reduce network latency from any applications that might have open connections. I'm used to using the Ctrl-Alt-Delete sequence, but since this is always hooked by the Winlogin.exe on the LOCAL computer, Remote Desktop can't transmit it over the connection.

Now that you know why - the solution! The control sequence Ctrl-Alt-End will act the same as a Ctrl-Alt-Delete when used in a Remote Desktop Session.


Repeating a Batch in SQL Server 2005

August 08, 2007 • 4:56PM • permalink
I haven't found this feature documented anywhere, but I picked it up from one of Itzik Ben-Gan's articles.

Normally, you use the keyword GO when you need to force the previous batch of code to immediately execute, such as when using DDL or CTEs, like so:


CREATE TABLE [dbo].[my_table]
(
   id INT
)

GO




While not useful in the case above, you can repeat a batch by placing an integer value immediately after the GO statement. This trick is very helpful when populating a table with random data or when doing repeated testing. So, for example, if we want to fill the above table with 1,000,000 rows of random integers, we can easily do it!



INSERT INTO [dbo].[my_table]
   (id)
VALUES
   (FLOOR(RAND() * 10000))

GO 1000000






page 1 of 1
1 




Tags

string parsing Mikomicon love server lazy initialization Windows c plus plus testing hack book review anime convention OS network Generic Method launch AlternativeNicheNetwork module Erlang PHP PC parsing script generation RegEx internals languages performance AnimeConPics SQL Flash OOP tools Stopwatch programming optimization development open source JSP shortcut mathematics protocol syntax dotnet Remote Desktop Generics new site CosplayWar SQL Server 2005 functional programming math bitwise