Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 带(无锁)GO的rom制造成本T1 USE [FSDBGL] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo_Sql - Fatal编程技术网

Sql 带(无锁)GO的rom制造成本T1 USE [FSDBGL] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo

Sql 带(无锁)GO的rom制造成本T1 USE [FSDBGL] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo,sql,Sql,带(无锁)GO的rom制造成本T1 USE [FSDBGL] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Mfg_ITMMAST]( [IMPN] [varchar](30) NOT NULL, [IMDESC] [varchar](70) NOT NULL, [IMUPCCD] [varchar](13) NOT NULL, ) ON [P

带(无锁)GO的rom制造成本T1
USE [FSDBGL]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Mfg_ITMMAST](
[IMPN] [varchar](30) NOT NULL,
[IMDESC] [varchar](70) NOT NULL,
[IMUPCCD] [varchar](13) NOT NULL,
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
USE [stackoverflow]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
--DROP TABLE [dbo].[Mfg_ITMMAST];
CREATE TABLE [dbo].[Mfg_ITMMAST](
    [IMPN] [varchar](30) NOT NULL,
    [IMDESC] [varchar](70) NOT NULL,
    [IMUPCCD] [varchar](13) NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'40000-01', N'test', N'601040')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'41023-01', N'test', N'601040123456')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'41001-02', N'test', N'601040')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'51001-01', N'test', N'601040')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'51001-02', N'test', N'601040')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'51014-02', N'test', N'601040234567')
INSERT [dbo].[Mfg_ITMMAST] ([IMPN], [IMDESC], [IMUPCCD]) VALUES (N'61001-01', N'test', N'601040')
CREATE PROCEDURE uspScanForBlankUpcs
AS

  -- setup variables for bringing in blank row data
  DECLARE @IMPN [varchar](30), @IMUPCCD [varchar](13),
          @blankUpc [varchar](13), @upcPrefix [varchar](6),
          @random [varchar](6), @retryRandom bit;
  SET @blankUpc = '601040'; -- This is the value of IMUPCCD when it is "blank"
  SET @upcPrefix = '601040'; -- This is prefix for our randomly generated UPC

  -- setup the cursor, query for items with "blank" UPCs
  DECLARE blanksCursor CURSOR FOR
  SELECT IMPN
  FROM [Mfg_ITMMAST]
  WHERE (LEFT(IMPN, 5) >= '40000' AND
         LEFT(IMPN, 5) < '60000' AND
         RIGHT(IMPN, 2) IN ('01','02')) AND
        IMUPCCD = @blankUpc
  ;

  -- open the cursor
  OPEN blanksCursor;

  -- load the next row from the cursor
  FETCH NEXT FROM blanksCursor
  INTO @IMPN;

  -- loop through each row of the cursor
  WHILE @@FETCH_STATUS = 0
  BEGIN
    --PRINT 'IMPN: ' + @IMPN;
    -- try to create a new random number
    SET @retryRandom = 1;
    WHILE @retryRandom = 1
    BEGIN
      -- get a random number for the UPC, then left-pad it with zeros to 6 digits
      SET @random = RIGHT('00000' + CONVERT(VARCHAR, FLOOR(RAND() * 999999)), 6);
      -- concatenate the UPC prefix with the random number
      SET @IMUPCCD = @upcPrefix + @random
      --PRINT 'IMUPCCD: ' + @IMUPCCD;
      -- see if this UPC already exists on another item
      IF (SELECT COUNT(*) FROM [Mfg_ITMMAST] WHERE [IMUPCCD] = @IMUPCCD) > 0
        SET @retryRandom = 1; -- UPC already existed (collision) try again
      ELSE
        SET @retryRandom = 0; -- didn't already exist, so exit out of loop
    END

    --PRINT 'Updating...';
    -- Update the UPC with the random number
    UPDATE [Mfg_ITMMAST]
    SET IMUPCCD = @IMUPCCD
    WHERE IMPN = @IMPN
    ;

    -- Load the next result
    FETCH NEXT FROM blanksCursor
    INTO @IMPN;

  END
  CLOSE blanksCursor;
  DEALLOCATE blanksCursor;

GO
exec uspScanForBlankUpcs;
--  Function to output UPC code based on Company Prefix and Item Reference:

CREATE FUNCTION [dbo].[calc_UPC] 
   (@company_prefix varchar(10), @item_reference int)
RETURNS char(12)
WITH SCHEMABINDING
AS
BEGIN
declare 
    @upc char(12),
    @checkdigit int

if SUBSTRING(@company_prefix, 1, 1) = 0 
begin

    set @upc = substring(@company_prefix, 2, 10) + right('000000000000' + ltrim(str(@item_reference)), 12 - len(@company_prefix))
    set @checkdigit = (1000 - (
        convert(int, substring(@upc, 1, 1)) * 3 +
        convert(int, substring(@upc, 2, 1)) * 1 +
        convert(int, substring(@upc, 3, 1)) * 3 +
        convert(int, substring(@upc, 4, 1)) * 1 +
        convert(int, substring(@upc, 5, 1)) * 3 +
        convert(int, substring(@upc, 6, 1)) * 1 +
        convert(int, substring(@upc, 7, 1)) * 3 +
        convert(int, substring(@upc, 8, 1)) * 1 +
        convert(int, substring(@upc, 9, 1)) * 3 +
        convert(int, substring(@upc, 10, 1)) * 1 +
        convert(int, substring(@upc, 11, 1)) * 3)) % 10

    set @upc = rtrim(@upc) + ltrim(str(@checkdigit))
end
return @upc
END
GO

-- Example Table of products:

CREATE TABLE [dbo].[Product](
    [product_id] [int] IDENTITY(1,1) NOT NULL,
    [company_prefix] [varchar](10) NULL,
    [item_reference] [int] NULL,
    [upc]  AS ([dbo].[calc_UPC]([company_prefix],[item_reference])) PERSISTED,
 CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
(
    [product_id] ASC
))
ALTER TABLE [dbo].[Product]  WITH CHECK ADD  CONSTRAINT [item_reference_greater_equal_zero] CHECK  (([item_reference]>=(0)))
GO
ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [item_reference_greater_equal_zero]
GO

-- Existing records with UPC codes:

insert product (company_prefix, item_reference) values ( '0601040', 3)
insert product (company_prefix, item_reference) values ( '0601040', 5)

-- Example of 4 new products without UPC codes

insert product DEFAULT VALUES
insert product DEFAULT VALUES
insert product DEFAULT VALUES
insert product DEFAULT VALUES
GO

-- Next we need a table of all possible item references.
-- This is the best implementation I have found for generating numbers:


--Creates a table of sequential numbers, useful for all sorts of things
--Created 08/26/05 by Oskar Austegard from article at 
--http://msdn.microsoft.com/library/en-us/dnsqlpro03/html/sp03k1.asp
--Limits: @Min and @Max must be between -2147483647 and 2147483647, including.
--If @Max <= @Min, only a single record with @Min is created
CREATE FUNCTION [dbo].[NumberTable] (@Min int, @Max int)
RETURNS @T TABLE (Number int NOT NULL PRIMARY KEY)
AS
BEGIN
  -- Seed the table with the min value
  INSERT @T VALUES (@Min)
  --Loop until all the rows are created, inserting ever more records for each iteration (1, 2, 4, etc)
  WHILE @@ROWCOUNT > 0
    BEGIN
      INSERT @T 
      --Get the next values by adding the current max - start value + 1 to each existing number
      --need to calculate increment value first to avoid arithmetic overflow near limits of int
      SELECT t.Number + (x.MaxNumber - @Min + 1)
      FROM @T t
        CROSS JOIN (SELECT MaxNumber = MAX(Number) FROM @T) x --Current max
      WHERE
        --Do not exceed the Max - shift the increment to the right side to take advantage of index
        t.Number <= @Max - (x.MaxNumber - @Min + 1)
    END
  RETURN
END

GO

-- For 10,000 numbers the performance of this function is good, 
-- but when the range is known I prefer the performance I get with a static table:
-- Create a table of numbers between 0 and 99999
CREATE table Numbers (number int)
insert Numbers (number)
select n.Number
from dbo.NumberTable(0, 99999) n

-- Now we can easily assign UPC codes using the available item reference values in your Company Prefix in a single update:

declare @company_prefix varchar(10) 
set @company_prefix = '0601040' -- The function requires the leading zero
update
    p
set
    item_reference = n.number,
    company_prefix = @company_prefix
from
    (
        select
            p.product_id,
            ROW_NUMBER() OVER (order by product_id) [row]
        from 
            dbo.product p
        where
            p.company_prefix is null
    ) u 
    inner join dbo.product p on p.product_id = u.product_id
    inner join
    (
        select
            s.Number,
            ROW_NUMBER() over (order by s.Number) [row]
        from
            (
                select n.Number from    
                (   
                    select 
                        n.Number
                    from
                        dbo.Numbers n --Table(@sequence, @size - 1) n
                        left outer join dbo.Product p 
                            on p.company_prefix = @company_prefix 
                                and n.Number = p.item_reference
                    where
                        p.product_id is null
                ) n
            ) s
    ) n on n.[row] = u.[row]
GO
select * from product
CREATE FUNCTION [dbo].[calc_EAN13] 
   (@company_prefix varchar(10), @item_reference int)
RETURNS char(13)
WITH SCHEMABINDING
AS
BEGIN
declare 
    @ean13 char(13),
    @checkdigit int

set @ean13 = @company_prefix
set @ean13 = @company_prefix + right('0000000000000' + ltrim(str(@item_reference)), 12 - len(@company_prefix))
set @checkdigit = (1000 - (
    convert(int, substring(@ean13, 1, 1)) * 1 +
    convert(int, substring(@ean13, 2, 1)) * 3 +
    convert(int, substring(@ean13, 3, 1)) * 1 +
    convert(int, substring(@ean13, 4, 1)) * 3 +
    convert(int, substring(@ean13, 5, 1)) * 1 +
    convert(int, substring(@ean13, 6, 1)) * 3 +
    convert(int, substring(@ean13, 7, 1)) * 1 +
    convert(int, substring(@ean13, 8, 1)) * 3 +
    convert(int, substring(@ean13, 9, 1)) * 1 +
    convert(int, substring(@ean13, 10, 1)) * 3 +
    convert(int, substring(@ean13, 11, 1)) * 1 +
    convert(int, substring(@ean13, 12, 1)) * 3)) % 10

set @ean13 = rtrim(@ean13) + ltrim(str(@checkdigit))
return @ean13
END
GO