Sunday, November 20, 2011

Find the error in following




Subject: Find the error, its impossible
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


 Did you know that 80% of UCDS students could not find the error above?
 Forward this to at least 5 people with the title 'Find the error, its impossible', and when you click 'Send', the answer will be right in front of your eyes!



Monday, January 10, 2011

How reducing the sql server database log file


 1.                 Login to the SQL server 2005 under "sa" login. Then run the following command
2.         Take a full backup of the database.
3.         Now run the following.

USE <write the database name>;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE <write the database name>
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 10 MB.
DBCC SHRINKFILE (<Database Logical Name>, 10);
GO
-- Reset the database recovery model.
ALTER DATABASE <write the database name>
SET RECOVERY FULL;
GO

NOTE : if the database recovery model was originally SIMPLE, then change it to SIMPLE after running the above script.
But if it is originally "FULL", then no need to change it again. Because the above script automatically change it to "FULL".



Sunday, October 3, 2010

How To Obtain The Size Of All Tables In A SQL Server Database

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   REPLACE(data,'KB','')  FROM   #t --order by data  

alter table  #t add data_1 numeric(18,2) 




UPDATE   #t SET data_1=convert(numeric(18,2),REPLACE(data,'KB',''))





SELECT *   FROM   #t order by data_1


 
DROP TABLE #t 

Thursday, September 30, 2010

How to export the data to existing EXCEL file from the SQL Server table


http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926
     
       
IF  EXISTS (SELECT srv.name FROM sys.servers srv WHERE srv.server_id != 0 AND srv.name = N'XLS')EXEC master.dbo.sp_dropserver @server=N'XLS', @droplogins='droplogins'
GO
    
EXEC sp_addlinkedserver N'XLS', 'Jet 4.0','Microsoft.Jet.OLEDB.4.0','c:\testing.xls',NULL,'Excel 5.0;

declare @text VARCHAR(400)

SET @text='HBS '

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=c:\testing.xls;',  'SELECT * FROM [Sheet1$]') select EMP_NUMBER, emp_calling_name ,@text TEXT from DSI.hs_hr_employee --where  emp_number='000001'


Tuesday, December 15, 2009

Essential SQL Server Date, Time and DateTime Functions

Part I: Standard Date and Time Functions

I've posted some variations of these before, but here they all are in 1 easy package:  The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any formatting considerations at all.

They are simple, easy, and brief and you should use them any time you need to incorporate any date literals or date math in your T-SQL code.  I have always wondered why T-SQL omits these basic functions, but the beauty of user defined functions is that we can create them ourselves.

create  function DateOnly(@DateTime DateTime)
-- Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value.
returns datetime
as
    begin
    return dateadd(dd,0, datediff(dd,0,@DateTime))
    end
go

create function Date(@Year int, @Month int, @Day int)
-- returns a datetime value for the specified year, month and day
-- Thank you to Michael Valentine Jones for this formula (see comments).
returns datetime
as
    begin
    return dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1)
    end
go

create function Time(@Hour int, @Minute int, @Second int)
-- Returns a datetime value for the specified time at the "base" date (1/1/1900)
-- Many thanks to MVJ for providing this formula (see comments). 

returns datetime
as
    begin
    return dateadd(ss,(@Hour*3600)+(@Minute*60)+@Second,0)
    end
go

create function TimeOnly(@DateTime DateTime)
-- returns only the time portion of a DateTime, at the "base" date (1/1/1900)
-- Thanks, Peso! 
returns datetime
as
    begin
    return dateadd(day, -datediff(day, 0, @datetime), @datetime)
    end
go

create function DateTime(@Year int, @Month int, @Day int, @Hour int, @Minute int, @Second int)
-- returns a dateTime value for the date and time specified.
returns datetime
as
    begin
    return dbo.Date(@Year,@Month,@Day) + dbo.Time(@Hour, @Minute,@Second)
    end
go


Remember that you must prefix UDFs with the owner (usually dbo) when calling them.

Usage Examples:
  •  where TransactionDate >= dbo.Date(2005,1,2)  -- no formatting or implicit string conversions needed for date literals
  • select dbo.Date(year(getdate()), 1,1) -- returns the first day of the year for the current year.
  • select dbo.DateOnly(getdate()) -- returns only the date portion of the current day.
  • if dbo.TimeOnly(SomeDate) = dbo.Time(5,30,0)  -- check to see if the time for a given date is at 5:30 AM
  • select dbo.Date(year(getdate()), month(getdate()),1) -- returns the first day of the current month.
  • select dbo.Date(year(getdate()), month(getdate())+1,0) -- returns the last day of the current month.
  • where SomeDate >= dbo.DateOnly(getdate()) and SomeDate < dbo.DateOnly(getDate())+1 -- a simple way to get all transactions that occurred on the current date
  • select dbo.DateOnly(getdate()) + 1 + dbo.Time(14,30,0) -- returns tomorrow at 2:30 PM.
and so on ....