/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Saturday, September 17, 2011

Triggers in sql server 2005

Some useful query related to Trigger in SQL SERVER 2005


***** how to create triggers in sql server 2005 ? *****


Create trigger your_trigger_name ON your_table_name
FOR Your_Operation -- For example : For Update or For Insert, For Delete, After Update, After Insert etc
AS
BEGIN
-- Your Input Output Parameter Declaration
SELECT @PARA1 = (COLUMN_NAME) FROM INSERTED
SELECT @PARA2 = (COLUMN_NAME) FROM DELETED
-- Your DATA Manipulation Operation Here
INSERT, UPDATE , DELETE
END
GO

Example Here:
 
FOR UPDATE TRIGGER IN SQL SERVER 2005


CREATE TRIGGER AfterUpdateTriggerTest ON TriggerTblCustomer
FOR UPDATE
AS
DECLARE @field AS INT
DECLARE @field1 AS VARCHAR(100)
BEGIN
    SELECT @field = (PKCUSTID) FROM INSERTED
    SELECT @field1 = (FNAME) FROM INSERTED
    UPDATE S SET name = @field1
    FROM TriggerTblSalesman AS S
    JOIN TriggerTblCustomer AS C
    ON C.PKCUSTID = S.FKCUSTID WHERE C.PKCUSTID = @field
END
GO



***** Search trigger text in SQL Server 2005 *****


SELECT text FROM syscomments WHERE [text] LIKE '%trigger%' AND
-- if you want a trigger attached to specific table then
[text] LIKE '%On your_trigger_name%'


SP_HELPTEXT your_trigger_name


***** Find all triggers in database *****

SELECT * FROM sys.triggers

***** Delete trigger from database *****

DROP TRIGGER your_trigger_name
Read More »

Monday, July 25, 2011

Using Captcha Image in Claasic asp

Read More »

Friday, July 22, 2011

SQL SERVER 2005: Expot to Excel using OPENROWSET

Apart from using DTS and Export wizard, we can also use this query to export data from SQL Server2000 to Excel

Create an Excel file named testing having the headers same as that of table columns and use these queries

1 Export data to existing EXCEL file from SQL Server table
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable

2 Export data from Excel to new SQL Server table
select *
into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')

3 Export data from Excel to existing SQL Server table
Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')

4 If you dont want to create an EXCEL file in advance and want to export data to it, use
EXEC sp_makewebtask
@outputfile = 'd:\testing.xls',
@query = 'Select * from Database_name..SQLServerTable',
@colheaders =1,
@FixedFont=0,@lastupdated=0,@resultstitle='Testing details'
Read More »

SQL SERVER 2005: How to enable ‘Ad Hoc Distributed Queries’ SQL SERVER 2005

Enabling  ‘Ad Hoc Distributed Queries’ SQL SERVER 2005
Frequently, we need to use OPENROWSET queries to connect to remote database servers. To enable this feature on SQL Server 2005, you should first configure the database to enable Ad Hoc Distributed Queries.
We can Enable this feature by two ways:
1. SQL Server Surface Area Configuration.
2. by sp_configure option.
Lets check with first way, by SQL Server Surface Area Configuration.
Open surface Area configuration, you will get this screen:
131
Click on second option, Surface Areas Configuration for Features. you will get this screen, where you need to check to Enable OPENROWSET and OPENDATASOURCE support.
132
Lets see second option to enable this feature with sp_configure option:
sp_configure
If you run this command, you will lists of SQL configuration settings. There are 14 items in the list in which ‘Ad Hoc Distributed Queries’ is not exist. To see this, we need to enable the ‘show advanced options’ configuration parameter.
You can enable advance options by:
sp_configure ‘show advanced options’,1
When we run this command we will get this message:
“Configuration option ‘show advanced options’ changed from 0 to 1. Run the RECONFIGURE statement to install.”
so we need to execute reconfigure command as:
reconfigure
so now if we run sp_configure again, we will get result set as follows:
133
Here, we can find that config_value for “Ad Hoc Distributed Queries” is “0”. We need to set it to 1 to enable this feature. so to do that we need to use following:
sp_configure ‘Ad Hoc Distributed Queries’,1
reconfigure
so, now if we run sp_configure, we will get result as follows:
134
Here, we can find that now config_value for “Ad Hoc Distributed Queries” is “1”.
That’s it, now you can use OPENROWSET and OPENDATASOURCE to connect with remote database without Linked server.
Let me know if it helps you in any way.
Read More »

Wednesday, June 22, 2011

T-SQL to change schema/owner name in Sql Server

Read More »

My Blog List