home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / i386 / web.sql < prev    next >
Encoding:
Text File  |  1995-12-14  |  15.9 KB  |  503 lines

  1. -- WEB.SQL 
  2.  
  3. -- Creates sp_makewebpage, which does essentially the same thing as the 
  4. -- Web Page Wizard, except it does not create triggers. 
  5.  
  6.  
  7. if exists (select * from sysobjects where id = object_id('dbo.sp_makewebpage') and sysstat & 0xf = 4)
  8.    drop procedure dbo.sp_makewebpage
  9. go
  10.  
  11. create procedure sp_makewebpage
  12. @outputfile   varchar(255),
  13. @query1       varchar(255),
  14. @query2       varchar(255) = NULL,
  15. @query3       varchar(255) = NULL,
  16. @query4       varchar(255) = NULL,
  17. @fixedfont    tinyint = 1,             -- 0/1 
  18. @bold         tinyint = 0,             -- 0/1 
  19. @italic       tinyint = 0,             -- 0/1 
  20. @colheaders   tinyint = 1,             -- 0/1 
  21. @lastupdated  tinyint = 1,             -- 0/1 
  22. @HTMLheader   tinyint = 1,             -- 1-6 
  23. @username_    varchar(30) = 'dbo',
  24. @dbname       varchar(30) = NULL,
  25. @templatefile varchar(255) = '?',
  26. @webpagetitle varchar(255) = 'Microsoft SQL Server Web Page Wizard',
  27. @resultstitle varchar(255) = 'Query Results',
  28. @URL          varchar(255) = '?',
  29. @reftext      varchar(255) = '?',
  30. @table_urls   tinyint = 0,             -- 0/1; 1=use table of URLs 
  31. @url_query    varchar(255) = NULL,   
  32. @whentype     tinyint = 1,             -- 1=now, 2=later, 3=every xday 
  33.                                        -- 4=every n units of time 
  34. @targetdate   int = -1,                -- yyyymmdd as int 
  35. @targettime   int = -1,                -- hhnnss as int 
  36. @dayflags     tinyint = 1,             -- powers of 2 for days of week 
  37. @numunits     tinyint = 1,
  38. @unittype     tinyint = 1,             -- 1=hours, 2=days, 3=weeks 
  39. @procname     varchar(28) = NULL,      -- name to use when making the
  40.                                        -- task and the wrapper/condenser
  41.                                        -- stored procs
  42. @maketask     bit = 1                  -- make a task? or just make the
  43.                                        -- stored procs?
  44. as
  45. begin
  46.    
  47.    declare @rc int
  48.    declare @taskname varchar(24)
  49.    declare @yearchar varchar(2)
  50.    declare @monthchar varchar(2)
  51.    declare @daychar varchar(2)
  52.    declare @hourchar varchar(2)
  53.    declare @minchar varchar(2)
  54.    declare @secchar varchar(2)
  55.    declare @currdate datetime
  56.    declare @freqtype_ int
  57.    declare @freqinterval_ int
  58.    declare @freqsubtype_ int
  59.    declare @freqsubinterval_ int
  60.    declare @freqrelativeinterval_ int
  61.    declare @freqrecurrencefactor_ int
  62.    declare @activestartdate_ int
  63.    declare @activestarttimeofday_ int
  64.    declare @complete varchar(255)
  65.    declare @buf varchar(255)
  66.    declare @taskid int
  67.    declare @suid smallint
  68.    declare @myusername varchar(30)
  69.    declare @sspid varchar(20)
  70.  
  71.    declare @i              tinyint
  72.    declare @j              tinyint
  73.    declare @c              char(1)
  74.    declare @newquery1      varchar(255)   
  75.    declare @newquery2      varchar(255)   
  76.    declare @newquery3      varchar(255)   
  77.    declare @newquery4      varchar(255)   
  78.    declare @ch_fixedfont   char(1)
  79.    declare @ch_bolditalic  char(1)
  80.    declare @ch_colheaders  char(1)
  81.    declare @ch_lastupdated char(1)
  82.    declare @ch_HTMLheader  char(1)
  83.  
  84. -- check args 
  85.  
  86.    if ((@fixedfont <> 0) and (@fixedfont <> 1))
  87.    begin
  88.       RAISERROR(16947,11,0)
  89.       return (1)
  90.    end
  91.    if ((@bold <> 0) and (@bold <> 1))
  92.    begin
  93.       RAISERROR(16948,11,0)
  94.       return (2)
  95.    end
  96.    if ((@italic <> 0) and (@italic <> 1))
  97.    begin
  98.       RAISERROR(16949,11,0)
  99.       return (3)
  100.    end
  101.    if ((@colheaders <> 0) and (@colheaders <> 1))
  102.    begin
  103.       RAISERROR(16950,11,0)
  104.       return (4)
  105.    end
  106.    if ((@lastupdated <> 0) and (@lastupdated <> 1))
  107.    begin
  108.       RAISERROR(16951,11,0)
  109.       return (5)
  110.    end
  111.    if ((@HTMLheader < 1) or (@HTMLheader > 6))
  112.    begin
  113.       RAISERROR(16952,11,0)
  114.       return (6)
  115.    end
  116.    select @rc = 0
  117.    exec @rc = sp_validname @username_
  118.    if (@rc = 1)
  119.    begin
  120.       RAISERROR(16954,11,0)
  121.       return (8)
  122.    end
  123.    if (@dbname != NULL)
  124.       if (not(exists(select * from master..sysdatabases where name = @dbname)))
  125.       begin
  126.          RAISERROR(16955,11,0)
  127.          return (9)
  128.       end
  129.    if ((@whentype < 1) or (@whentype > 4))
  130.    begin
  131.       RAISERROR(16956,11,0)
  132.       return (10)
  133.    end
  134.    if ((@unittype < 1) or (@unittype > 3))
  135.    begin
  136.       RAISERROR(16957,11,0)
  137.       return (11)
  138.    end
  139.    if ((@whentype = 2) and (@targetdate < 19000101))
  140.    begin
  141.       RAISERROR(16958,11,0)
  142.       return (12)
  143.    end
  144.    if (((@whentype = 2) or (@whentype = 3)) and (@targettime = -1))
  145.    begin
  146.       RAISERROR(16959,11,0)
  147.       return (13)
  148.    end
  149.    if ((@whentype = 3) and (@dayflags = 0))
  150.    begin
  151.       RAISERROR(16960,11,0)
  152.       return (14)
  153.    end
  154.    if ((@whentype = 3) and (@numunits < 1))
  155.    begin
  156.       RAISERROR(16961,11,0)
  157.       return (15)
  158.    end
  159.    if ((@whentype = 4) and (@numunits < 1))
  160.    begin
  161.       RAISERROR(16962,11,0)
  162.       return (16)
  163.    end
  164.    if ((@whentype = 4) and ((@unittype < 1) or (@unittype > 3)))
  165.    begin
  166.       RAISERROR(16963,11,0)
  167.       return (17)
  168.    end
  169.    if ( ((@URL = '?') and (@reftext <> '?')) or
  170.         ((@URL <> '?') and (@reftext = '?')) or
  171.         ((@table_urls = 1) and (@url_query is null)) or
  172.         ((@table_urls = 0) and (@url_query is not null)) )
  173.    begin
  174.       RAISERROR(16964,11,0)
  175.       return (18)
  176.    end
  177.  
  178. -- override the @username_ value if suid != 1 (sa)
  179. -- if @dbname is NULL, supply a default
  180.  
  181.    select @suid = suid from master..sysprocesses where spid = @@spid
  182.  
  183.    if ((@suid != 1) or (@dbname = NULL))
  184.    begin
  185.       select @sspid = convert(varchar(20),@@spid)
  186.       select @dbname = d.name from
  187.       master..sysdatabases d, master..sysprocesses p
  188.       where d.dbid = p.dbid
  189.          and spid = @@spid
  190.       exec("insert into msdb..MSWork (spid,value1) select @@spid, name from "+@dbname+"..sysusers u, master..sysprocesses p where u.uid = p.uid and spid = "+@sspid)
  191.       if (@suid != 1)
  192.          select @username_ = convert(varchar(30),value1) from msdb..MSWork
  193.       delete from msdb..MSWork
  194.       where spid = @@spid
  195.    end
  196.  
  197.    if (@procname=NULL)
  198.       begin
  199.          select @currdate = getdate()
  200.          select @yearchar = substring(convert(varchar(4),datepart(yy,@currdate)),3,2)
  201.          select @monthchar = replicate("0",2-datalength(convert(varchar(2),datepart(mm,@currdate))))+convert(varchar(2),datepart(mm,@currdate))
  202.          select @daychar = replicate("0",2-datalength(convert(varchar(2),datepart(dd,@currdate))))+convert(varchar(2),datepart(dd,@currdate))
  203.          select @hourchar = replicate("0",2-datalength(convert(varchar(2),datepart(hh,@currdate))))+convert(varchar(2),datepart(hh,@currdate))
  204.          select @minchar = replicate("0",2-datalength(convert(varchar(2),datepart(mi,@currdate))))+convert(varchar(2),datepart(mi,@currdate))
  205.          select @secchar = replicate("0",2-datalength(convert(varchar(2),datepart(ss,@currdate))))+convert(varchar(2),datepart(ss,@currdate))
  206.          select @procname = "Web_"+@yearchar+@monthchar+@daychar+@hourchar+@minchar+@secchar+convert(varchar(20),@@spid)
  207.       end
  208.  
  209.    select @taskname=@procname
  210.    select @complete="exec "+@procname+";2"
  211.  
  212.    if (@whentype = 1)   -- now 
  213.    begin
  214.       select @freqtype_ = 2
  215.       select @freqinterval_ = 0
  216.       select @freqsubtype_ = 1
  217.       select @freqsubinterval_ = 0
  218.       select @freqrelativeinterval_ = 1
  219.       select @freqrecurrencefactor_ = 1
  220.       select @activestartdate_ = 0
  221.       select @activestarttimeofday_ = 0
  222.       -- one-time tasks kill themselves & their stored procs
  223.       select @complete = @complete + " drop procedure "+@procname+" exec msdb..sp_droptask '"+@taskname+"'"
  224.    end
  225.  
  226.    if (@whentype = 2)   -- later 
  227.    begin
  228.       select @freqtype_ = 1
  229.       select @freqinterval_ = 0
  230.       select @freqsubtype_ = 1
  231.       select @freqsubinterval_ = 0
  232.       select @freqrelativeinterval_ = 1
  233.       select @freqrecurrencefactor_ = 1
  234.       select @activestartdate_ = @targetdate
  235.       select @activestarttimeofday_ = @targettime
  236.       -- one-time tasks kill themselves & their stored procs
  237.       select @complete = @complete + " drop procedure "+@procname+" exec msdb..sp_droptask '"+@taskname+"'"
  238.    end
  239.  
  240.    if (@whentype = 3)   -- every xday 
  241.    begin
  242.       select @freqtype_ = 8
  243.       select @freqinterval_ = @dayflags
  244.       select @freqsubtype_ = 1
  245.       select @freqsubinterval_ = 0
  246.       select @freqrelativeinterval_ = 1
  247.       select @freqrecurrencefactor_ = 1
  248.       select @activestartdate_ = 0
  249.       select @activestarttimeofday_ = @targettime
  250.    end
  251.  
  252.    if ((@whentype = 4) and (@unittype = 1))  -- every n hours 
  253.    begin
  254.       select @freqtype_ = 4
  255.       select @freqinterval_ = 1
  256.       select @freqsubtype_ = 8
  257.       select @freqsubinterval_ = @numunits
  258.       select @freqrelativeinterval_ = 1
  259.       select @freqrecurrencefactor_ = 1
  260.       select @activestartdate_ = 0
  261.       select @activestarttimeofday_ = 0
  262.    end
  263.  
  264.    if ((@whentype = 4) and (@unittype = 2))  -- every n days 
  265.    begin
  266.       select @freqtype_ = 4
  267.       select @freqinterval_ = @numunits
  268.       select @freqsubtype_ = 1
  269.       select @freqsubinterval_ = 0
  270.       select @freqrelativeinterval_ = 1
  271.       select @freqrecurrencefactor_ = 1
  272.       select @activestartdate_ = 0
  273.       select @activestarttimeofday_ = @targettime
  274.    end
  275.  
  276.    if ((@whentype = 4) and (@unittype = 3))  -- every n weeks 
  277.    begin
  278.       select @freqtype_ = 8
  279.       select @freqinterval_ = @dayflags
  280.       select @freqsubtype_ = 1
  281.       select @freqsubinterval_ = 0
  282.       select @freqrelativeinterval_ = 1
  283.       select @freqrecurrencefactor_ = @numunits
  284.       select @activestartdate_ = 0
  285.       select @activestarttimeofday_ = @targettime
  286.    end
  287.  
  288.    select @ch_fixedfont=convert(char(1),@fixedfont)  
  289.    select @ch_bolditalic=convert(char(1),@bold+2*@italic)
  290.    select @ch_colheaders=convert(char(1),@colheaders)
  291.    select @ch_lastupdated=convert(char(1),@lastupdated)
  292.    select @ch_HTMLheader=convert(char(1),@HTMLheader)
  293.  
  294.    if (@table_urls = 1)
  295.    begin
  296.        select @URL = '~TABLE~'
  297.        select @reftext = @url_query
  298.    end
  299.  
  300.  
  301.    select @j = datalength(@query1)
  302.    select @i = 1
  303.    select @newquery1 = ""
  304.    select @buf = ""
  305.  
  306.    while (@i <= @j)
  307.    begin
  308.       select @c = substring(@query1,@i,1)
  309.  
  310.       if (@c = char(13))
  311.          select @c = char(11)
  312.       else
  313.          if (@c = char(10))
  314.             select @c = char(12)
  315.          else
  316.             if (@c = "'")
  317.                select @c = char(8)
  318.             else
  319.                if (@c = '"')
  320.                   select @c = char(9)
  321.                else
  322.                   if (@c = " ")
  323.                   begin
  324.                      select @newquery1 = @newquery1 + @buf
  325.                      select @buf = ""
  326.                   end
  327.  
  328.       select @buf = @buf + @c
  329.       select @i = @i + 1
  330.    end
  331.  
  332.    if (@buf != "")
  333.       select @newquery1 = @newquery1 + @buf
  334.  
  335.    if (@query2=NULL)
  336.       select @newquery2=NULL
  337.    else
  338.       begin
  339.          select @j = datalength(@query2)
  340.          select @i = 1
  341.          select @newquery2 = ""
  342.          select @buf = ""
  343.  
  344.          while (@i <= @j)
  345.          begin
  346.             select @c = substring(@query2,@i,1)
  347.  
  348.             if (@c = char(13))
  349.                select @c = char(11)
  350.             else
  351.                if (@c = char(10))
  352.                   select @c = char(12)
  353.                else
  354.                   if (@c = "'")
  355.                      select @c = char(8)
  356.                   else
  357.                      if (@c = '"')
  358.                         select @c = char(9)
  359.                      else
  360.                         if (@c = " ")
  361.                         begin
  362.                            select @newquery2 = @newquery2 + @buf
  363.                            select @buf = ""
  364.                         end
  365.  
  366.             select @buf = @buf + @c
  367.             select @i = @i + 1
  368.          end
  369.  
  370.          if (@buf != "")
  371.             select @newquery2 = @newquery2 + @buf
  372.       end
  373.  
  374.    if (@query3=NULL)
  375.       select @newquery3=NULL
  376.    else
  377.       begin
  378.          select @j = datalength(@query3)
  379.          select @i = 1
  380.          select @newquery3 = ""
  381.          select @buf = ""
  382.  
  383.          while (@i <= @j)
  384.          begin
  385.             select @c = substring(@query3,@i,1)
  386.  
  387.             if (@c = char(13))
  388.                select @c = char(11)
  389.             else
  390.                if (@c = char(10))
  391.                   select @c = char(12)
  392.                else
  393.                   if (@c = "'")
  394.                      select @c = char(8)
  395.                   else
  396.                      if (@c = '"')
  397.                         select @c = char(9)
  398.                      else
  399.                         if (@c = " ")
  400.                         begin
  401.                            select @newquery3 = @newquery3 + @buf
  402.                            select @buf = ""
  403.                         end
  404.  
  405.             select @buf = @buf + @c
  406.             select @i = @i + 1
  407.          end
  408.  
  409.          if (@buf != "")
  410.             select @newquery3 = @newquery3 + @buf
  411.       end
  412.  
  413.  
  414.    if (@query4=NULL)
  415.       select @newquery4=NULL
  416.    else
  417.       begin
  418.          select @j = datalength(@query4)
  419.          select @i = 1
  420.          select @newquery4 = ""
  421.          select @buf = ""
  422.  
  423.          while (@i <= @j)
  424.          begin
  425.             select @c = substring(@query4,@i,1)
  426.  
  427.             if (@c = char(13))
  428.                select @c = char(11)
  429.             else
  430.                if (@c = char(10))
  431.                   select @c = char(12)
  432.                else
  433.                   if (@c = "'")
  434.                      select @c = char(8)
  435.                   else
  436.                      if (@c = '"')
  437.                         select @c = char(9)
  438.                      else
  439.                         if (@c = " ")
  440.                         begin
  441.                            select @newquery4 = @newquery4 + @buf
  442.                            select @buf = ""
  443.                         end
  444.  
  445.             select @buf = @buf + @c
  446.             select @i = @i + 1
  447.          end
  448.  
  449.          if (@buf != "")
  450.             select @newquery4 = @newquery4 + @buf
  451.       end
  452.  
  453.  exec("CREATE PROCEDURE "+@procname+" WITH ENCRYPTION AS "+@newquery1+@newquery2+@newquery3+@newquery4)
  454.  if (not exists (select 1 from sysobjects where type = 'P' and name = @procname)) return(26)
  455.  
  456.  
  457.    exec("CREATE PROCEDURE "+@procname+";2 WITH ENCRYPTION AS exec master..xp_cmdshell 'mssqlwpw "+@username_+"!"+@dbname+"!"+@ch_fixedfont+@ch_bolditalic+@ch_colheaders+@ch_lastupdated+@ch_HTMLheader+"0!"+@templatefile+"!"+@webpagetitle+
  458.    "!"+@resultstitle+"!"+@URL+"!"+@reftext+"!"+@outputfile+"!exec "+@procname+"'")
  459.  
  460.   
  461.    if (@maketask = 1)
  462.    begin
  463.       select @rc = 0
  464.       exec @rc = msdb..sp_addtask @name=@taskname, @subsystem="TSQL",
  465.            @server=NULL, @username=@username_, @databasename=@dbname,
  466.            @enabled=1,@freqtype=@freqtype_, @freqinterval=@freqinterval_,
  467.            @freqsubtype=@freqsubtype_, @freqsubinterval=@freqsubinterval_,
  468.            @freqrelativeinterval=@freqrelativeinterval_,
  469.            @freqrecurrencefactor=@freqrecurrencefactor_,
  470.            @activestartdate=@activestartdate_, @activeenddate=99991231,
  471.            @activestarttimeofday=@activestarttimeofday_,
  472.            @activeendtimeofday=235900,
  473.            @runpriority=0, @emailoperatorname=NULL,
  474.            @retryattempts=1, @retrydelay=5, @command=@complete,
  475.            @loghistcompletionlevel=2, @emailcompletionlevel=0,
  476.            @description=NULL, @tagadditionalinfo=NULL, @tagobjectid=NULL,
  477.            @tagobjecttype=NULL
  478.  
  479.       if (@rc > 0)
  480.       begin
  481.           RAISERROR(16948,11,0,@rc)
  482.           return (27)
  483.       end
  484.    end
  485.  
  486.    if (@maketask = 1) and (@whentype = 1)
  487.       begin
  488.          select @taskid = id from msdb..systasks where name = @taskname
  489.          if ((@taskid = NULL) or (@taskid = 0))
  490.             RAISERROR(16965,11,0)
  491.          else
  492.          begin
  493.             exec @rc = sp_schedulersignal 'O', @taskid
  494.             if (@rc != 0)
  495.                RAISERROR(16966,11,0,@rc)
  496.          end
  497.       end
  498. end
  499. go
  500.  
  501. grant execute on sp_makewebpage to public
  502. go
  503.