6 Months Free & No Setup Fees ASP.NET Hosting!
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » SQL Server » They are created one stored procedure but i can't understand.
       
Author Reply
magesh manavalan
posted 157 posts
since Sep 17, 2010 
from

They are created one stored procedure but i can't understand.

  Posted on: 30 Jan 2012       


They are created one stored procedure but i can't understand.if any know knows this stored procedure pls Explain what functionality the are used and tell me with examples.





SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO




Create PROC SearchAndReplace
(
@SearchStr nvarchar(100),
@ReplaceStr nvarchar(100)
)
AS
BEGIN



SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110), @SQL nvarchar(4000), @RCTR int
SET @TableName = ''
SET @SearchStr2 = QUOTENAME( @SearchStr ,'''')
SET @RCTR = 0

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ANDQUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
ANDOBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) and (@TableName != 'eLogs_Master_Company')
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA= PARSENAME(@TableName, 2)
ANDTABLE_NAME= PARSENAME(@TableName, 1)
ANDDATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
ANDQUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
if(@TableName <> '[dbo].[eLogs_Master_Company]')

SET @SQL='UPDATE ' + @TableName +
' SET ' + @ColumnName
+ ' = REPLACE(' + @ColumnName + ', '
+ QUOTENAME(@SearchStr, '''') + ', ' + QUOTENAME(@ReplaceStr, '''') +
') WHERE ' + @ColumnName + ' = ' + @SearchStr2

EXEC (@SQL)
SET @RCTR = @RCTR + @@ROWCOUNT
END
END
END

SELECT CAST(@RCTR AS varchar)
END


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



Thanks&Regs

Magesh.A

Ronal Parmar
posted  5 posts
since  Apr 16, 2012 
from 

 Re: They are created one stored procedure but i can't understand.
  Posted on: 23 Apr 2012        0  


Dear,

        This procedure only works for 'char', 'varchar', 'nchar', 'nvarchar' Datatypes.

        In This Procedure you can replace Any Fields of above datatypes cell values of any of the table
         of your current database.

        Try to check below example
        Where I create one table with 2varchar fields And one Autogenerated primary key.
        Then i passed value of last cell 'Ajeet' And  new value 'Ajit' So procedure itself search the value And Replace by your second
        Value.
        i.e. 'Ajeet' Replaced by 'Ajit'


-- Create Table
CREATE TABLE [dbo].[TblUsers](
    [UId] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [UName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Pwd] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
) ON [PRIMARY]

GO
-- Insert Values
INSERT INTO [dbo].[TblUsers] ([UName],[Pwd]) VALUES('Tedulkar','Sachin')
INSERT INTO [dbo].[TblUsers] ([UName],[Pwd]) VALUES('Sewag','Virendra')
INSERT INTO [dbo].[TblUsers] ([UName],[Pwd]) VALUES('Ganguli','Saurav')
INSERT INTO [dbo].[TblUsers] ([UName],[Pwd]) VALUES('Dhoni','Mahi')
INSERT INTO [dbo].[TblUsers] ([UName],[Pwd]) VALUES('Agarkar','Ajeet')

GO
-- Check Records
Select * From TblUsers
GO
-- Execute Procedure
Exec SearchAndReplace @SearchStr = 'Ajeet', @ReplaceStr = 'Ajit'

GO
-- Again Check Records To see Updation
Select * From TblUsers


Try it and let me know if you want to understand more.

Its great logical thing to change same Records of whole database at once execution.
Thank You & Regards
Ronal

       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved