Wednesday, March 7, 2012

Create an INSERT Script from an existing Table to populate an existing table

Seems as if SQL Server doesn't have a readily available built-in tool for this, so thank you Nigel Rivett.


if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_CreateDataLoadScript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_CreateDataLoadScript]
GO

Create Procedure sp_CreateDataLoadScript
@TblName varchar(128)
as
/*
exec sp_CreateDataLoadScript 'MyTable'
*/


 create table #a (id int identity (1,1), ColType int, ColName varchar(128))

 insert  #a (ColType, ColName)
 select case when DATA_TYPE like '%char%' then 1 else 0 end ,
  COLUMN_NAME
 from  information_schema.columns
 where  TABLE_NAME = @TblName
 order by ORDINAL_POSITION
 
 if not exists (select * from #a)
 begin
  raiserror('No columns found for table %s', 16,-1, @TblName)
  return
 end

declare @id int ,
 @maxid int ,
 @cmd1 varchar(7000) ,
 @cmd2 varchar(7000)

 select  @id = 0 ,
  @maxid = max(id)
 from  #a

 select @cmd1 = 'select '' insert ' + @TblName + ' ( '
 select @cmd2 = ' + '' select '' + '
 while @id < @maxid
 begin
  select @id = min(id) from #a where id > @id

  select  @cmd1 = @cmd1 + ColName + ','
  from #a
  where id = @id

  select @cmd2 =  @cmd2
    + ' case when ' + ColName + ' is null '
    + ' then ''null'' '
    + ' else '
    +   case when ColType = 1 then  ''''''''' + ' + ColName + ' + ''''''''' else 'convert(varchar(20),' + ColName + ')' end
    + ' end + '','' + '
  from #a
  where id = @id
 end


 select @cmd1 = left(@cmd1,len(@cmd1)-1) + ' ) '' '
 select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblName

 select '/*' + @cmd1 + @cmd2 + '*/'

 exec (@cmd1 + @cmd2)
 drop table #a

go

Thursday, January 12, 2012

JQuery Version check

In web dev tools console (for whatever browser you are using), just issue the following command:

$().jquery;

Wednesday, November 30, 2011

LINQ to SQL HA HA HA

When contemplating the motivation for doing away with the impedance created between an Object Oriented app and a Relational Database, and thus ultimately LINQ and Entity Framework, I would struggle to feel convinced (I'm equally comfortable writing T-SQL in Management Studio as I am writing VB and C# in Visual Studio).

If anything stood out in my mind as inconvenient, it would certainly be the tedious and redundant variable/field names when writing queries.  I was always adept at solving such problems with a good text editor at my side that could use vertical highlighting and Regular Expressions (such as Textpad), so this wasn't that big a deal.  But nonetheless, I found (and am still finding) the 2008 SQL Server Management Studio's Intellisense capabilities (FINALLY!!) to be amazingly helpful.  So helpful that, if I did have any desires to lower the impedance between a OOP architected app and an RDBMS, the 2008 tool would have satisfied it.

So, this is the irony.  The tool's improvements were released the SAME TIME as the company (Microsoft) was pushing LINQ to SQL and Entity Framework.

And this seems a bit absurd to me.

But wait, there's more...“LINQ to SQL is dead!”


Monday, September 26, 2011

1.x DataGrid, 2.0 GridView, Declarative Syntax in HTML portion...

"In Microsoft ASP.NET 1.x, one of the most commonly used ASP.NET Web controls is the DataGrid. The DataGrid control makes displaying data on an ASP.NET page a breeze: simply drag and drop the DataGrid onto your ASP.NET page, specify the columns and formatting, and write just a few lines of source code to retrieve and bind the data to the DataGrid. With a bit more effort and code, you could enable the end user to page, sort, and even edit the data within the DataGrid.


While the DataGrid was a huge improvement over pre-ASP.NET techniques for displaying data, it still suffered some limitations. For starters, binding any data to a DataGrid required some code writing, and often repetitive code at that. Furthermore, utilizing any of the DataGrid's more advanced features—handling deletes, allowing for editing of the underlying data, providing pagination, or sorting support—required additional code and time. Granted, the code for many of these additions was not terribly complex or lengthy, but any time code is written the possibility of new bugs, typos, or errors is introduced.

ASP.NET 2.0 fixes the problems of the DataGrid through a set of data source controls designed to encapsulate data that can be bound to data Web controls, and a replacement for the DataGrid—the GridView control. To access data in ASP.NET 1.x, developers had to write code; with the data source controls, however, data can be accessed through declarative syntax in the HTML portion of the ASP.NET Web page. Like other ASP.NET controls, the data source controls can be added to a page by simply dragging and dropping them from the Microsoft Visual Studio Toolbox onto the ASP.NET page Design view. Additionally, the specific data to retrieve can be specified entirely through the Design view. Once a data source control has been configured, it can be bound to a data Web control, such as the GridView, by setting the data Web control's DataSourceID property to the ID of the data source control. That's all there is to it—no code necessary!"

http://msdn.microsoft.com/en-us/library/aa479340.aspx

Tuesday, September 20, 2011

Basic IFrame Use with JavaScript

Here is the simplest example:

<html>
<body>
<iframe id="smedly" src="http://www.google.com"></iframe>
<input type="button" onclick="dothis();" value="Yahoo">
<script>
function dothis(){
document.getElementById('smedly').setAttribute('src','http://www.yahoo.com');
}
</script>
</body>
</html>



Friday, August 5, 2011

jQuery toggleClass with Visibility

I'm not sure why jQuery uses the CSS property "display" to toggle hide and show, since "display" when set to "none" (which jQuery.hide() does) removes the space it takes up, and if you have elements adjacent, they're going to move around.

The workaround is to use toggleClass with a class you create which is used exclusively for setting "visibility" to "hidden" (instead of "visible"). When an element is "hidden", it is still there taking up space, keeping other elements properly aligned, it just is invisible.

The typical example of the use case is when you have a blank area that when hovered over displays some tool icons, or a delete X.

However, you might run into a confusing problem if you try this without giving the element to be toggled on and off the invisible class to begin with.
Ex.:

//style
.hidden{
visibility: hidden;
}
//HTML
<span class='hidden del'>X</span>
......
//jQuery
$('.editbay').hover(function(){
$(this).find('span').toggleClass('hidden');
});

Friday, July 29, 2011

Firebug musts

for(x in console)
{
console.log(console[x].name)
};

console.dir(console);