Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. USE [lime_crm_base_solution_v7_0_1]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[csp_lc_runonsqlupdate] Script Date: 2019-09-19 14:01:33 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- Written by: Fredrik Eriksson
  9. -- Created: 2016-09-29
  10.  
  11. -- ##########
  12. -- DISCLAIMER:
  13. -- This procedure is a built in procedure in Lime Core.
  14. -- Modifying it could have consequences beyond your imagination.
  15. -- Or not.
  16. -- Seriously though, you should always make sure that no other built in functionality breaks if you must modify this procedure.
  17. -- ##########
  18.  
  19. -- Use this procedure to recalculate fields that have onsqlupdate expressions set in LISA.
  20.  
  21. -- @@tablename: (Mandatory) The database name of the table to update.
  22. -- @@fieldnames: (Not mandatory) A semicolon separated string with the database names of the fields to update.
  23. -- Can be left empty if all fields with onsqlupdate expressions on the specified table should be recalculated.
  24. -- @@whereclause: (Not mandatory) Use this string to specify conditions that have to apply for a record to be updated.
  25. -- For example: Only open helpdesk records, use 'enddate IS NULL'.
  26. -- @@printsql: (Not mandatory) Set to 1 if you want the procedure to only print the SQL generated statement instead of executing it.
  27.  
  28. ALTER PROCEDURE [dbo].[csp_lc_runonsqlupdate]
  29. @@tablename NVARCHAR(64)
  30. , @@fieldnames NVARCHAR(1024) = N''
  31. , @@whereclause NVARCHAR(1024) = N''
  32. , @@printsql BIT = 1
  33. AS
  34. BEGIN
  35.  
  36. -- FLAG_EXTERNALACCESS --
  37.  
  38.  
  39. DECLARE @sql NVARCHAR(MAX)
  40. SET @sql = N''
  41.  
  42. SET @sql = N'UPDATE [' + @@tablename + N']' + CHAR(10) + N'SET [status] = [status]' + CHAR(10)
  43.  
  44. SELECT @sql = @sql + CHAR(9) + N', [' + f.[name] + N'] = (' + a.[value] + N')' + CHAR(10)
  45. FROM [attributedata] a
  46. INNER JOIN [field] f
  47. ON a.[idrecord] = f.[idfield]
  48. INNER JOIN [table] t
  49. ON f.[idtable] = t.[idtable]
  50. LEFT JOIN [dbo].[cfn_lc_gettablefromstring](@@fieldnames, N';') fn
  51. ON fn.[value] = f.[name]
  52. WHERE a.[owner] = N'field'
  53. AND a.[name] = N'onsqlupdate'
  54. AND t.[name] = @@tablename
  55. AND
  56. (
  57. @@fieldnames = N''
  58. OR fn.[value] IS NOT NULL
  59. )
  60.  
  61. SET @sql = @sql + N'WHERE [status] = 0'
  62. + CASE
  63. WHEN @@whereclause <> N''
  64. THEN CHAR(10) + CHAR(9) + N'AND (' + @@whereclause + N')'
  65. ELSE N''
  66. END
  67.  
  68. IF @@printsql = 1
  69. BEGIN
  70. PRINT @sql
  71. END
  72. ELSE
  73. BEGIN
  74. EXECUTE(@sql)
  75. END
  76. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement