Tuesday, February 15, 2011

SQL SERVER – Query to Find ByteSize of All the Tables in Database

I Am writing here two ways to find the database table size

Method One 1:
---------------------------------------------------

SELECT
CASE WHEN (GROUPING(sob.name)=1) THEN 'All_Tables'
ELSE ISNULL(sob.name, 'unknown') END AS Table_name,
SUM(sys.length) AS Byte_LengthFROM sysobjects sob, syscolumns sysWHERE sob.xtype='u' AND sys.id=sob.idGROUP BY sob.nameWITH CUBE


Method One 2:

---------------------------------------------------
Ever wonder how big a table really is in your database? You know there are a million rows in the table, but how much space is that really taking?

SQL Server provides a built-in stored procedure that you can run to easily show the size of a table, including the size of the indexes… which might surprise you.
Syntax:   sp_spaceused ‘Tablename’

Method One 3:
---------------------------------------------------
Actually SQL Server gives you everything you need with its Stored Procedure sp_spaceused. Unfortunately this SP does not support iterating over all tables in a database, so we needed to leverage another (undocumented) Stored Procedure sp_msForEachTable.

SET NOCOUNT ON 
DBCC UPDATEUSAGE(0) 
-- DB size.
EXEC sp_spaceused

-- Table row counts and sizes.
CREATE TABLE #t 
( 
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18), 
    data VARCHAR(18), 
    index_size VARCHAR(18),
    unused VARCHAR(18)
) 

INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' 

SELECT *
FROM   #t

-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM   #t
 
DROP TABLE #t 
 
 
Method One 4:
---------------------------------------------------
CREATE PROCEDURE GetAllTableSizes
AS
/*
Obtains spaced used data for ALL user tables in the database
*/
DECLARE @TableName VARCHAR(100) --For storing values in the cursor
--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
select [name]
from dbo.sysobjects
where OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY
--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
--Open the cursor
OPEN tableCursor
--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName
--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
--Dump the results of the sp_spaceused query to the temp table
INSERT #TempTable
EXEC sp_spaceused @TableName
--Get the next table name
FETCH NEXT FROM tableCursor INTO @TableName
END
--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor
--Select all records so we can use the reults
SELECT *
FROM #TempTable
--Final cleanup!
DROP TABLE #TempTable
GO

Exec GetAllTableSizes

Wednesday, February 2, 2011

Bind Repeater form sql datasource

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
        onitemdatabound="Repeater1_ItemDataBound">
    <HeaderTemplate>
        <table border="1" cellpadding="5px">
    </HeaderTemplate>

    <ItemTemplate>
            <!-- Image -->
            <tr>
                <td colspan="2">
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImgId", "~/Image.aspx?id={0}") %>' />
                </td>
            </tr>
            <!-- Message/Date -->
            <tr>
                <td>
                    <%# Eval("ImgMessage"%>
                </td>
                <td>
                    <%#Eval("ImgDate""{0:d}")%>
                </td>
            </tr>
    </ItemTemplate>

    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Images]"></asp:SqlDataSource>

Monday, January 31, 2011

An in depth discussion of JavaScript Arrays - Working with two dimensional arrays: discussion

Within the code in the previous section, I mainly created a simple button (which is identified as “ButtonShow”). The button is defined with an “onclick” event which calls a JavaScript function “ButtonShow_onclick”, which is defined as follows:

function ButtonShow_onclick() {
Show();
}

The above function simply calls another JavaScript function named “Show.” The function “Show” is defined as follows:

function Show()
{
var ITArray = new Array(2300, 3105, 2909, 4800);
var ProductionArray = new Array(1800, 1940, 2470, 4350);
var ResearchArray = new Array(900, 1200, 1923, 3810);
var salaryArray = new Array(ITArray, ProductionArray, ResearchArray);

for (var i = 0; i < salaryArray.length; i++) {
for (var j = 0; j < salaryArray[i].length; j++) {
document.write(salaryArray[i][j] + "\t");
}
document.write("
")
}
}

Before starting this discussion, we need to understand what we are creating (in memory) from the above code. Let me provide you a logical view for the array (“salaryArray”) in the above code. Consider the following figure (Fig 01):

Just consider the above figure as a small part of memory identified by “salaryArray.” All the black numbers correspond to values. All the reds are access notations (the positions) for the values given in black. Every access notation is a combination of a row index (in blue) and a column index (in green).

We are trying to create three single dimensional arrays as part of the main array, and thus we describe the main array as a “two dimensional” array (which is very similar to the concept of matrices in mathematics).

According to the above code, the three single dimensional arrays (or rows) are “ITArray”, “ProductionArray” and “ResearchArray. All of them have their own values (but the number of values in all of them is generally the same). We created a main array named “salaryArray” which contains the three arrays in the form of a list (thus forming a table type of view).

Finally, we retrieve and display all the values in the “salaryArray” with the following nested loop:

for (var i = 0; i < salaryArray.length; i++) {
for (var j = 0; j < salaryArray[i].length; j++) {
document.write(salaryArray[i][j] + "\t");
}
document.write("
")
}

With the above code fragment, the variable “i” corresponds to the “row index” and the variable “j” corresponds to the “column index.”

Thursday, January 27, 2011

Get Last Executed Query with Time

Using below query you can easily trace the query that are executed earlier..


Select dmStats.last_execution_time as 'Last Executed Time',dmText.text as 'Executed Query' from sys.dm_exec_query_stats as dmStats Cross apply sys.dm_exec_sql_text(dmStats.sql_handle) as dmText Order By dmStats.last_execution_time desc

Monday, January 24, 2011

SQL Split Function


This SQL Split Function is use to SPLIT a sentences based on the Delimeter.
Delimeter is a string character used to identify substring limits.

Below is Split Function in SQL
DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='SQL,TUTORIALS'
SET @Delimiter = ','
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)
END


Result
- SQL
- TUTORIALS

* Copy blue color Sql split function with change @String variable to test the result in your query analyzer.



Unable to connect to asp.net development server


I got this error after updating Microsoft Visual Studio 2008 Service Pack 1 (which took much longer than expected - close to two hours):

WebDev.WebServer.exe has stopped working
Unable to connect to the ASP.NET Development Server

As usual, this appeared to be a problem many people had, under many different scenarios, with many different solutions. The following solution is the solution that worked for me:

Delete the web project from the solution, rename the folder where the website is stored and then re-add the project to the solution as an existing website.

This is fine on my local laptop, but would probably be a bit more disconcerting in Visual Source Safe.

So my best guess is Visual Studio stores information about a web site somewhere.  If you don't perform the folder rename, when you add the web site back in you'll see the original, dynamic port settings.  It appears renaming forces a new port.

I don't know where this information is stored.  I can't seem to find anything in the registry, and deleting the temporary asp.net files didn't have any affect.

Although renaming the folder worked, I also did this:

The host file at %\Windows\System32\drivers\etc\hosts may have an incorrect entry. This line...

::1 localhost

was changed to...

:::1 localhost

That's three colons - not two.

I didn't experiment any further and have just moved on.


Sunday, January 2, 2011

Set Class From Javascript


//change the tab class style
var browser=navigator.appName;
var classtyp;
if(browser=='Microsoft Internet Explorer')
{
classtyp = "className";
}
else
{
classtyp = "class";
}

if (val=='new')
{
document.getElementById("liUpcoming").setAttribute(classtyp, "selectednews");
document.getElementById("liPast").setAttribute(classtyp, "");
}