home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / msmqocm.cab / CREATEDB.SQL < prev    next >
Text File  |  1997-11-10  |  9KB  |  846 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.   
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517. create procedure msmq_check_existence
  518.     (@DatabaseName  varchar(255))
  519. as
  520.  
  521.  
  522.     
  523.     
  524.     
  525.     declare @DatabaseExists bit
  526.     select @DatabaseExists = 0
  527.     select @DatabaseExists = 1
  528.         from sysdatabases
  529.         where name = @DatabaseName
  530.     if (@DatabaseExists = 1)
  531.     begin
  532.         return(184)
  533.     end
  534.  
  535.  
  536.     
  537.     
  538.     
  539.     declare @DeviceExists bit
  540.     select @DeviceExists = 0
  541.  
  542.     select @DeviceExists = 1
  543.         from sysdevices
  544.         where name = @DatabaseName + 'Data'
  545.     if (@DeviceExists = 1)
  546.     begin
  547.         return(187)
  548.     end
  549.  
  550.     select @DeviceExists = 1
  551.         from sysdevices
  552.         where name = @DatabaseName + 'Log'
  553.     if (@DeviceExists = 1)
  554.     begin
  555.         return(191)
  556.     end
  557.  
  558.  
  559.     return(2)
  560. go
  561.  
  562. create procedure msmq_create_device
  563.     (@ISQLPath              varchar(255),
  564.      @DeviceName            varchar(255),
  565.      @DeviceDirectory       varchar(255),
  566.      @DeviceSize            int,
  567.      @CANNOT_CREATE_DEVICE  int,
  568.      @DEVICE_EXISTS         int, 
  569.      @DEVICE_IN_USE         int,
  570.      @FILE_EXISTS           int
  571.      )
  572. as
  573.     declare @retcode int
  574.  
  575.     if @@microsoftversion < 0x07000000 
  576.         exec @retcode = msmq_create_device_6 @ISQLPath, 
  577.             @DeviceName,
  578.             @DeviceDirectory,
  579.             @DeviceSize, 
  580.             @CANNOT_CREATE_DEVICE,
  581.                 @DEVICE_EXISTS, 
  582.             @DEVICE_IN_USE,
  583.             @FILE_EXISTS
  584.     else
  585.             exec @retcode = msmq_create_device_7 @ISQLPath, 
  586.             @DeviceName,
  587.             @DeviceDirectory,
  588.             @DeviceSize, 
  589.             @CANNOT_CREATE_DEVICE,
  590.                 @DEVICE_EXISTS, 
  591.             @DEVICE_IN_USE,
  592.             @FILE_EXISTS
  593.         
  594.         return @retcode
  595.  
  596. go
  597.  
  598.  
  599. create procedure msmq_create_device_6
  600.     (@ISQLPath              varchar(255),
  601.      @DeviceName            varchar(255),
  602.      @DeviceDirectory       varchar(255),
  603.      @DeviceSize            int,
  604.      @CANNOT_CREATE_DEVICE  int,
  605.      @DEVICE_EXISTS         int, 
  606.      @DEVICE_IN_USE         int,
  607.      @FILE_EXISTS           int
  608.      )
  609. as
  610.  
  611.  
  612.     
  613.     
  614.     
  615.     declare @DeviceAlreadyUsed bit
  616.     select @DeviceAlreadyUsed = 0
  617.     select @DeviceAlreadyUsed = 1
  618.         from sysusages
  619.         where vstart between
  620.             (select low from sysdevices where name = @DeviceName) and
  621.             (select high from sysdevices where name = @DeviceName)
  622.     if (@DeviceAlreadyUsed = 1)
  623.     begin
  624.         return(@DEVICE_IN_USE)
  625.     end
  626.  
  627.  
  628.     
  629.     
  630.     
  631.     declare @DeviceExists bit
  632.     select @DeviceExists = 0
  633.     select @DeviceExists = 1
  634.         from sysdevices
  635.         where name = @DeviceName
  636.     if (@DeviceExists = 1)
  637.     begin
  638.         return(@DEVICE_EXISTS)
  639.     end
  640.  
  641.  
  642.     
  643.     
  644.     
  645.     declare @DevicePath varchar(255)
  646.     select @DevicePath = @DeviceDirectory + '\' + @DeviceName + '.dat'
  647.     declare @Command varchar(255)
  648.     select @Command = 'dir ' + @DevicePath
  649.     declare @DirectoryResult int
  650.     execute @DirectoryResult = xp_cmdshell @Command, no_output
  651.     if (@DirectoryResult = 0)
  652.     begin
  653.         return(@FILE_EXISTS)
  654.     end
  655.  
  656.  
  657.     
  658.     
  659.     
  660.     declare @VirtualDeviceNum int
  661.     select @VirtualDeviceNum = 0
  662.     select @VirtualDeviceNum = (min(low)/0x01000000)+1
  663.         from sysdevices SysDevices1
  664.         where low/0x01000000 between 0 and 254
  665.         and not exists
  666.             (select * from sysdevices SysDevices2
  667.                 where SysDevices2.low/0x01000000 = (SysDevices1.low/0x01000000)+1)
  668.     if (@VirtualDeviceNum = 0)
  669.     begin
  670.         return(185)
  671.     end
  672.  
  673.  
  674.     
  675.     
  676.     
  677.     select @Command =
  678.         @ISQLPath + ' -E -d master -Q "EXIT(' +
  679.             'disk init' +
  680.                 ' name = ' + @DeviceName +
  681.                 ', physname = ''' + @DevicePath + '''' +
  682.                 ', size = ' + convert(varchar(8), @DeviceSize * 512) +
  683.                 ', vdevno = ' + convert(varchar(3), @VirtualDeviceNum) + ')"'
  684.     declare @DeviceCreated int
  685.     execute @DeviceCreated = xp_cmdshell @Command, no_output
  686.  
  687.     
  688.     
  689.     
  690.     if (@DeviceCreated <> 0)
  691.     begin
  692.         select @Command = 'del ' + @DevicePath
  693.         execute xp_cmdshell @Command, no_output
  694.         return(@CANNOT_CREATE_DEVICE)
  695.     end
  696.  
  697.  
  698.     return(1)
  699. go
  700.  
  701. create procedure msmq_create_device_7
  702.     (@ISQLPath              varchar(255),
  703.      @DeviceName            varchar(255),
  704.      @DeviceDirectory       varchar(255),
  705.      @DeviceSize            int,
  706.      @CANNOT_CREATE_DEVICE  int,
  707.      @DEVICE_EXISTS         int, 
  708.      @DEVICE_IN_USE         int,
  709.      @FILE_EXISTS           int
  710.      )
  711. as
  712.         
  713.        
  714.     declare @DeviceExists bit
  715.     select @DeviceExists = 0
  716.     select @DeviceExists = 1
  717.         from sysdevices
  718.         where name = @DeviceName
  719.     if (@DeviceExists = 1)
  720.     begin
  721. select 'device exists'
  722.         return(@DEVICE_EXISTS)
  723.     end
  724.     
  725.     declare @DevicePath varchar(255)
  726.     select @DevicePath = @DeviceDirectory + '\' + @DeviceName + '.dat'
  727.     declare @Command varchar(255)
  728.     select @Command = 'dir ' + @DevicePath
  729.     declare @DirectoryResult int
  730.     execute @DirectoryResult = xp_cmdshell @Command, no_output
  731.     if (@DirectoryResult = 0)
  732.     begin
  733.         return(@FILE_EXISTS)
  734.     end
  735.     
  736.     
  737.     select @Command = 'disk init' +
  738.                 ' name = ' + @DeviceName +
  739.                 ', physname = ''' + @DevicePath + '''' +
  740.                 ', size = ' + convert(varchar(8), @DeviceSize * 512)
  741.  
  742.     exec (@Command)
  743.  
  744.     if @@error <> 0
  745.     begin
  746.         select @Command = 'del ' + @DevicePath
  747.         execute xp_cmdshell @Command, no_output
  748.         return(@CANNOT_CREATE_DEVICE)
  749.     end
  750.  
  751.     return(1)
  752. go
  753.  
  754. create procedure msmq_create_database
  755.     (@ISQLPath              varchar(255),
  756.      @DatabaseName          varchar(255),
  757.      @DataDeviceDirectory   varchar(255),
  758.      @DataDeviceSize        int,
  759.      @LogDeviceDirectory    varchar(255),
  760.      @LogDeviceSize         int)
  761. as
  762.  
  763.  
  764.     
  765.     
  766.     
  767.     declare @DatabaseExists bit
  768.     select @DatabaseExists = 0
  769.     select @DatabaseExists = 1
  770.         from sysdatabases
  771.         where name = @DatabaseName
  772.     if (@DatabaseExists = 1)
  773.     begin
  774.         return(184)
  775.     end
  776.  
  777.  
  778.     
  779.     
  780.     
  781.     
  782.     declare @DataDeviceName varchar(255)
  783.     select @DataDeviceName = @DatabaseName + 'Data'
  784.     declare @DeviceCreated int
  785.     execute @DeviceCreated = msmq_create_device
  786.                                 @ISQLPath,
  787.                                 @DataDeviceName,
  788.                                 @DataDeviceDirectory,
  789.                                 @DataDeviceSize,
  790.                                 186,
  791.                                 187, 
  792.                                 188,
  793.                                 189
  794.     if (@DeviceCreated <> 1)
  795.     begin
  796.         return(@DeviceCreated)
  797.     end
  798.  
  799.  
  800.     
  801.     
  802.     
  803.     declare @LogDeviceName varchar(255)
  804.     select @LogDeviceName = @DatabaseName + 'Log'
  805.     execute @DeviceCreated = msmq_create_device
  806.                                 @ISQLPath,
  807.                                 @LogDeviceName,
  808.                                 @LogDeviceDirectory,
  809.                                 @LogDeviceSize,
  810.                                 190,
  811.                                 191, 
  812.                                 192,
  813.                                 193
  814.     if (@DeviceCreated <> 1)
  815.     begin
  816.         execute sp_dropdevice @DataDeviceName, delfile
  817.         return(@DeviceCreated)
  818.     end
  819.  
  820.  
  821.     
  822.     
  823.     
  824.     declare @Command varchar(255)
  825.     select @Command =
  826.         'create database ' + @DatabaseName +
  827.             ' on ' + @DataDeviceName + ' = ' + convert(varchar(4), @DataDeviceSize) +
  828.             ' log on ' + @LogDeviceName + ' = ' + convert(varchar(4), @LogDeviceSize)
  829.     execute(@Command)
  830.     if (@@error <> 0)
  831.     begin
  832.         execute sp_dropdevice @DataDeviceName, delfile
  833.         execute sp_dropdevice @LogDeviceName, delfile
  834.         return(183)
  835.     end
  836.  
  837.  
  838.     
  839.     
  840.     
  841.     execute sp_dboption MQIS, 'trunc. log on chkpt.', true
  842.  
  843.  
  844.     return(0)
  845. go
  846.