use Dictionary
go
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
use Dictionary
go
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShorttag
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NOT NULL)
UPDATE tblwords
SET StrShorttag = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
use Dictionary
go
update TblWords
set StrShort=Rtrim(REPLACE(StrShort,' ',''))
update TblWords
set StrShort=LTRIM(REPLACE(StrShort,' ',''))
update TblWords
set StrShortTag=Rtrim(REPLACE(StrShortTag,' ',''))
update TblWords
set StrShortTag=LTRIM(REPLACE(StrShortTag,' ',''))
use Dictionary
go
CREATE PROCEDURE your_proc_name AS
/*
Meaningful code comments here
*/
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShorttag
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NOT NULL)
UPDATE tblwords
SET StrShorttag = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
update TblWords
set StrShort=Rtrim(REPLACE(StrShort,' ',''))
update TblWords
set StrShort=LTRIM(REPLACE(StrShort,' ',''))
update TblWords
set StrShortTag=Rtrim(REPLACE(StrShortTag,' ',''))
update TblWords
set StrShortTag=LTRIM(REPLACE(StrShortTag,' ',''))
GO
USE Dictionary
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_CreateStrStringsplusTidyup]
AS
BEGIN
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShorttag
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NOT NULL)
UPDATE tblwords
SET StrShorttag = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
update TblWords
set StrShort=Rtrim(REPLACE(StrShort,' ',''))
update TblWords
set StrShort=LTRIM(REPLACE(StrShort,' ',''))
update TblWords
set StrShortTag=Rtrim(REPLACE(StrShortTag,' ',''))
update TblWords
set StrShortTag=LTRIM(REPLACE(StrShortTag,' ',''))
END
GO
use Dictionary
go
CREATE PROCEDURE [dbo].[usp_CreateStrStringsPlusTidyup] AS
/*
Create StrStrings and tidy up after
*/
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NULL)
UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShorttag
FROM tblwords w
LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
WHERE wt.word IS NOT NULL)
UPDATE tblwords
SET StrShorttag = RTRIM((SELECT I.Word + ' '
FROM Filtered I
WHERE I.clientcode = tblwords.clientcode
ORDER BY i.WordPosition
FOR XML PATH('')))
update TblWords
set StrShort=Rtrim(REPLACE(StrShort,' ',''))
update TblWords
set StrShort=LTRIM(REPLACE(StrShort,' ',''))
update TblWords
set StrShortTag=Rtrim(REPLACE(StrShortTag,' ',''))
update TblWords
set StrShortTag=LTRIM(REPLACE(StrShortTag,' ',''))
GO
One of the goofyriffic quirks of T-SQL, and designed surely only so that Microsoft can have stupid questions on certification exams, is that you need a semi-colon immediately before any CTE..WITH statements.
Open in new window
And then you'd call the name of your stored procedure from your VB. In the case above, the stored procedure is called myStoredProcedure