Create DatabaseChaoshils--Create a databaseGo UseChaoshils--Use this databaseGoCreate TableGongying--Create a table for a vendor(Gcodeint Primary Key Identity(1001,1),--supplier number, primary keyGnamevarchar( -),--NameGsdhvarchar( -),--TelephoneGsdzvarchar( -)-- Address )--Add data to the Vendor tableInsert intoGongyingValues('Coca-Cola Company','232323','Qingdao, Shandong')Insert intoGongyingValues('Peanut Oil Co.','323232','Jinan, Shandong')Insert intoGongyingValues('Green Tea Company','565656','Rizhao, Shandong')Insert intoGongyingValues('Mineral Water company','656565','Shandong Weifang')Insert intoGongyingValues('hongxing Erguotou Co., Ltd.','878787','Beijing')Create TableChaoshi--Create a table for a supermarket(Ccodeint Primary Key,--Product numberCNamevarchar( -),--Product nameCshuint,--Number of productsCjindecimal( -,2),--Input priceCshoudecimal( -,2),--PriceCgcodeint --vendor number, foreign key for supplier table)--Add data to the supermarket tableInsert intoChaoshiValues(10001,'Coca Cola', -,2,3,1001)Insert intoChaoshiValues(10002,'Peanut Oil', -, +, the,1002)Insert intoChaoshiValues(10003,'Green Tea', -,8, -,1003)Insert intoChaoshiValues(10004,'Mineral Water', Max,1,2,1004)Insert intoChaoshiValues(10005,'Red Star Erguotou', -,3,5,1005)--Create a small ticket table, not directly inserted data, in the following stored procedure according to the sales of the product to add dataCreate TableXiaopiao (Xcodeint,--Product number, foreign key of supermarket tableXNamevarchar( -),--Product nameXshuint,--Number of products purchasedXjiagedecimal( -,2),--Product priceZjiadecimal( -,2)--Total Product)Select* fromgongyingSelect* fromChaoshiCreate procLiushui--Create a stored procedure to record the quantity price of the sold goods added to the Small ticket table@mai int,--Returns a parameter, the number of purchases@hcode int--The number of the item purchased asbeginDeclare--Declare using the variables defined below@name varchar( -),@shu int,@danjia decimal( -,2),@zong decimal( -,2),@count intSelect @name=CName fromChaoshiwhereCcode=@hcode--Find the product name in the supermarket by the product number of the return parameter assign value to the variable @nameSelect @shu=Cshu fromChaoshiwhereCcode=@hcode--by number to find the existing quantity of this product is assigned to the variable @shuSelect @danjia=Cshou fromChaoshiwhereCcode=@hcode--The price of this product is assigned to the variable @danjia by the numberSet @zong=@danjia*@mai--Goods price @danjia times the quantity purchased in the return parameter @mai equals the total price, use the variable @zong to receiveSelect @count=Count(*) fromChaoshiwhereCcode=@hcode--To see if the returned parameter product number exists, there is a value of 1, does not exist as 0, received in the variable @countif @count>0--If the @count is not 0, then there is this productbegin if @shu> = @mai--If the quantity of goods in the supermarket is greater than or equal to the quantity purchased begin Insert intoXiaopiaoValues(@hcode,@name,@mai,@danjia,@zong)--Add the purchase information to the Xiaopiao table SelectXcode asRef., XName asProduct Name, Xshu asPurchase quantity, Xjiage asUnit Price, Zjia asTotal fromXiaopiao--Query the information added to the table and change the column name to Chinese character display End Else if @shu<@mai--If the number of items in the supermarket is less than the quantity purchased begin Print'Insufficient Cargo' --Then, the goods are not enough End EndElse if @count=0--If the @count value is 0, there is no productbeginPrint'no such product'EndEndGo--Check to execute this stored procedure only onceexecLiushui -,10008--Here is the execution of the stored procedure Liushui and returns two parameters, quantity and product number, modifiable quantity and product parameters select other Items --Here the product number parameter in the table and there is this number of goods, then the implementation of @count=0 this step, there is no such product, so the small ticket table will not add the number
--according toSelect* fromXiaopiao--View the data stored in the Small ticket table
SQL stored procedure supermarket small ticket print