June 6, 2007 at 8:44 am
I'm connecting to a SQL 2000 database with SQL 2005 Mgmt Studio. I have a string field, that I don't want to allow commas in. This is a small table, and for now it is for internal use only, so there isn't even an interface for it outside of writing a query or opening the table in Manager.
I thought I could do this with an INSERT/UPDATE trigger, but I'm getting "The logical tables INSERTED and DELETED cannot be updated.". Are Contraints 2005 only? IF not, how do you use them, because I tried making one, but it didn't work, and doesn't show up when I refresh the Object Explorer. All I want to do is replace any occurrence of a comma in this field with a blank space, when it's updated or inserted. How do I do this?
June 6, 2007 at 9:22 am
You're probably better off either restricting the input in the application or replace commas with spaces before inserting or updating in a stored procedure. As an example of the latter, if the value to be inserted or updated is passed to a stored procedure as a parameter, you could use
insert into gctest
values (1,replace(string_parm,',', ' '))
where gctest is the table and string_parm is the string containing commas.
Greg
Greg
June 6, 2007 at 9:29 am
Thanks for the input. I might try the stored proc route, or just making a note of it for now. Like I said, for the immediate future, this tbale doesn't have any sort of application interface, and is only going to be worked with either using SQL statements or directly through Enterprise Manager opening and editing the table. Thanks again!
June 6, 2007 at 3:58 pm
CREATE TABLE tmp_test (
recid smallint
IDENTITY(1,1)
PRIMARY KEY CLUSTERED,
mycolumn varchar(50) NOT NULL )
create trigger tmp_test_trigger on tmp_test
for insert as
declare @id int
declare @index smallint
select @id = recid, @index = CHARINDEX(',', mycolumn) from INSERTED
if @index > 0
update tmp_test set mycolumn = replace(mycolumn,',','') where recid = @id
June 6, 2007 at 4:15 pm
So what happens when more than 1 row is inserted in the table?
June 6, 2007 at 4:54 pm
--===== If you have a table that looks like this... CREATE TABLE yourtable ( RowNum INT IDENTITY(1,1) PRIMARY KEY, SomeString VARCHAR(100) NULL ) GO --===== Add a constraint that looks like this ALTER TABLE yourtable ADD CONSTRAINT CK_yourtable CHECK (NOT(SomeString LIKE '%,%'))
--===== And I guarantee you won't be able to insert comma's like this... even directly... INSERT INTO yourtable (SomeString) VALUES ('comma,in,here')
--Jeff Moden
Change is inevitable... Change for the better is not.
June 6, 2007 at 6:07 pm
trigger will check for every row inserted
June 6, 2007 at 7:22 pm
I meant with more than 1 row inserted at the same time.
June 6, 2007 at 9:35 pm
Trigger will work if the input is by record, however for bulk input then you might have to use REPLACE(MyValue,',','') in the SQL that stores the info.
June 7, 2007 at 12:22 am
Use a trigger for AFTER INSERT UPDATE
declare a cursor for
select [id] from inserted where [comma_in_clause]
and update while step trough cursor
June 7, 2007 at 9:02 am
How about you search how to make a trigger work on a set of rows. I'm sure that this come in handy someday!!
June 7, 2007 at 10:56 am
You can use two INSTED OF triggers to do it.
Using Jeff's table definition (Thanks):
CREATE TABLE yourtable
(
RowNum INT IDENTITY(1,1) PRIMARY KEY,
SomeString VARCHAR(100) NULL
)
CREATE TRIGGER T1
ON yourtable
INSTEAD OF INSERT
AS
INSERT INTO yourtable (SomeString)
SELECT REPLACE(SomeString, ',', ' ') FROM inserted
CREATE TRIGGER T2
ON yourtable
INSTEAD OF UPDATE
AS
UPDATE yourtable
SET SomeString = REPLACE(I.SomeString, ',', ' ')
FROM yourtable Y
INNER JOIN inserted I ON Y.RowNum = I.RowNum
The advantage of this solution over Jeff's is that I think it matches the requirements of replacing the comma with a space when a user enters a string that contains a comma instead of aborting the operation.
---------------------------------------------
[font="Verdana"]Nothing is impossible.
It is just a matter of time and money.[/font]
June 7, 2007 at 11:01 am
Ahhhhh, intersting! Thank you, that looks like it might work!
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply