Pregunta

I've recently been asked to look into SQL for the first time and have come across an issue which i cant figure out and was hoping to seek help, i have two SQL servers both running Microsoft SQL Server 2008 and Server A has a linked server to Server B,

a trigger exists which sends data into a stored procedure which then passes the data on insert from server A to server B and this data has names in, the problem i am facing is that names like O'Neil are failing due to the apostrophe and i cant figure out how to replace the apostrophe with double apostrophe's when the data im harvesting is only available from the 'inserted' table.

my trigger is as follows:

 SELECT @PassengerName=PassengerName, @DocumentType=DocumentType, @BoardingSequenceNumber=BoardingSequenceNumber,
        @FlightNumber=FlightNumber, @CarrierCode=CarrierCode, @DepartureTime=DepartureTime, @Destination=Destination, @DeviceAddress=DeviceAddress, 
        @Timestamp="Timestamp", @WorkstationId=WorkstationId, @DeliveredTimeStamp=DeliveredTimeStamp from inserted;

SELECT @cmd = 'sqlcmd -S '+@@SERVERNAME+' -E -d '+DB_NAME()+' -Q "exec SP_transfer @PassengerName='''+@PassengerName+''',@DocumentType='+@DocumentType+
',@BoardingSequenceNumber='+cast(@BoardingSequenceNumber as varchar(20))+
',@FlightNumber='+@FlightNumber+',@CarrierCode='+@CarrierCode+
',@DepartureTime='''+cast(@DepartureTime as varchar(20))+''',@Destination='+@Destination+
',@DeviceAddress='+@DeviceAddress+''',@TimeStamp='+cast(@Timestamp as varchar(32))+
''',@WorkstationId='+@WorkstationId+',@DeliveredTimeStamp='''+cast(@DeliveredTimeStamp as varchar(50))+'''"';

how can i make it so that the passengername column replaces any apostrophes without breaking the trigger but before it attempts to fire? i appologies if this question seems stupid, but as i mentioned im pretty new to this line of work,

thank you for your time,

Craig

¿Fue útil?

Solución

Im not sure if you want to remove or replace the apostrophes in the select. There is a replace function in SQL replace(string, replacestring, replacewith)

SELECT  replace('test','est', 'aste')
returns 
Taste

So depending on the fields you are using you can strip out the apostrophes on the select of the trigger.

SELECT  
    @PassengerName=replace(PassengerName,'''',''),
    @DocumentType=replace(DocumentType,'''',''),
    @BoardingSequenceNumber=replace(BoardingSequenceNumber,'''',''),
    @FlightNumber=replace(FlightNumber,'''',''),
    @CarrierCode=replace(CarrierCode,'''',''),
    @DepartureTime=replace(DepartureTime,'''',''),
    @Destination=replace(Destination,'''',''),
    @DeviceAddress=replace(DeviceAddress,'''',''),
    @Timestamp="Timestamp",
    @WorkstationId=WorkstationId,
    @DeliveredTimeStamp=DeliveredTimeStamp 
from inserted;

If you don't want to remote the apostrophes you could double them up in the same way.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top