Author |
Topic |
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:16:43
|
I have a to generate a load of random data and need to populate the Value Column (Quantity Column * Price Column)But I don't know how to do it. See Script below. Can you help.DECLARE @OrderNumber varchar (30)DECLARE @OrderDate intDECLARE @OrderLineNumber varchar(50)DECLARE @CustomerSkey intDECLARE @ProductSkey intDECLARE @OrderMethodSkey intDECLARE @Quantity intDECLARE @Cost Decimal(18,3)SET @OrderNumber = 1SET @OrderDate = 0SET @OrderLineNumber = 1SET @CustomerSkey = 1SET @ProductSkey = 1SET @OrderMethodSkey = 1SET @Quantity = 1SET @Cost = 1WHILE @OrderNumber <= 100WHILE @OrderDate <= 100WHILE @OrderLineNumber <= 100WHILE @CustomerSkey <= 100WHILE @ProductSkey <= 100WHILE @OrderMethodSkey <= 100WHILE @Quantity <= 100WHILE @Cost <= 100BEGININSERT INTO Orders (OrderNumber, OrderDate, OrderLineNumber, CustomerSkey, ProductSkey, OrderMethodSkey, OrderTime, Quantity, Cost)SELECT 'ORD' + Right ('000000' + CAST (@OrderNumber AS varchar (30)), 6),DATEADD (day, CAST (RAND () * 1500 as int), '2008-1-1'),(Right ('0' + CAST (@OrderLineNumber AS varchar (30)), 6)),(99 * RAND()) + 1,(99 * RAND()) + 1,(2 * RAND()) + 1,DATEADD(ms, cast(86400000 * RAND() as int), convert(time, '00:00')),(190 * RAND()) + 10,(40 * RAND()) + 10SET @OrderNumber = @OrderNumber + 1SET @OrderDate = @OrderDate + 1SET @OrderLineNumber = @OrderLineNumber + 1SET @CustomerSkey = @CustomerSkey + 1SET @ProductSkey = @ProductSkey + 1SET @OrderMethodSkey = @OrderMethodSkey + 1SET @Quantity = @Quantity + 1SET @Cost = @Cost + 1END |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:26:59
|
so didnt understand. Is your attempt to populate a random set of data to your table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:28:30
|
Yes, I am creating a load of random data for testing purposes |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:30:13
|
Ok. But where's the Value column? I cant see it listed in INSERT column list------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:32:47
|
Hi VisakhIts not in there at the moment, I took it out because I couldn't figure out what to do.Ta |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:36:45
|
quote: Originally posted by wafw1971 Hi VisakhIts not in there at the moment, I took it out because I couldn't figure out what to do.Ta
Make it as a calculated column so that you dont have to add it in insert statement and value will get automatically calculated based on component columns.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:41:49
|
Hi VisakhI don't know how to that, the table is already set up so how do I change the Value column to a calculated column.TaWW |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:43:46
|
quote: Originally posted by wafw1971 Hi VisakhI don't know how to that, the table is already set up so how do I change the Value column to a calculated column.TaWW
use ALTER TABLE statementALTER TABLE YourTableName ALTER COLUMN [Value] AS Quantity * Price ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:46:51
|
I am getting the following error messageMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'AS'. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:51:22
|
[code]ALTER TABLE YourTableName DROP COLUMN [Value] GOALTER TABLE YourTableName ADD [Value] AS (Quantity * Price)GO[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 04:56:56
|
I know have this error message: Msg 1759, Level 16, State 0, Line 1Computed column 'Price' in table 'Orders' is not allowed to be used in another computed-column definition.This is because my Price column is Cost column +35% |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 04:58:49
|
quote: Originally posted by wafw1971 I know have this error message: Msg 1759, Level 16, State 0, Line 1Computed column 'Price' in table 'Orders' is not allowed to be used in another computed-column definition.This is because my Price column is Cost column +35%
then you need to replace Price with its definitionieALTER TABLE YourTableName DROP COLUMN [Value] GOALTER TABLE YourTableName ADD [Value] AS (Quantity * (Cost + ..))GO put actual calculation for Price field for code in blue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2013-05-23 : 05:01:51
|
Here's a slightly better way to do it:; WITH random_floats AS ( SELECT ROW_NUMBER() OVER (ORDER BY NEWID()) [r] , CAST('0.' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY(3)) AS INT)) AS VARCHAR(20)) AS FLOAT) AS [f] FROM (SELECT 1 AS [n] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) a CROSS JOIN (SELECT 1 AS [n] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) b CROSS JOIN (SELECT 1 AS [n] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) c CROSS JOIN (SELECT 1 AS [n] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) d ) ,random_data AS ( SELECT 'ORD' + RIGHT('000000' + CAST(rf.[r] AS varchar (30)), 6) AS [OrderNumber] , DATEADD (day, CAST (rf.[f] * 1500 as int), '2008-1-1') AS OrderDate , (Right ('0' + CAST (rf.[r] AS varchar (30)), 6)) AS OrderLineNumber , (99 * rf.[f]) + 1 AS CustomerSkey , (99 * rf.[f]) + 1 AS ProductSkey , (2 * rf.[f]) + 1 AS OrderMethodSkey , DATEADD(MS, cast(86400000 * rf.[f] as int), convert(time, '00:00')) AS [OrderTime] , (190 * rf.[f]) + 10 AS [Quantity] , (40 * rf.[f]) + 10 AS [Cost] FROM random_floats AS rf WHERE rf.[r] < 101 )SELECT * FROM random_data Results:OrderNumber OrderDate OrderLineNumber CustomerSkey ProductSkey OrderMethodSkey OrderTime Quantity Cost----------- ----------------------- --------------- ---------------------- ---------------------- ---------------------- ---------------- ---------------------- ----------------------ORD000001 2011-04-24 00:00:00.000 01 80.8281055 80.8281055 2.612689 19:21:08.1640000 163.205455 42.25378ORD000002 2008-08-18 00:00:00.000 02 16.23640987 16.23640987 1.30780626 03:41:37.2300000 39.2415947 16.1561252ORD000003 2008-07-13 00:00:00.000 03 13.84987033 13.84987033 1.25959334 03:06:54.4320000 34.6613673 15.1918668ORD000004 2009-08-25 00:00:00.000 04 40.7746756 40.7746756 1.8035288 09:38:32.4440000 86.335236 26.070576ORD000005 2008-08-18 00:00:00.000 05 16.20146188 16.20146188 1.30710024 03:41:06.7300000 39.1745228 16.1420048ORD000006 2008-08-14 00:00:00.000 06 15.94194526 15.94194526 1.30185748 03:37:20.2430000 38.6764606 16.0371496ORD000007 2008-07-10 00:00:00.000 07 13.63691341 13.63691341 1.25529118 03:03:48.5780000 34.2526621 15.1058236ORD000008 2008-06-07 00:00:00.000 08 11.48422276 11.48422276 1.21180248 02:32:29.8670000 30.1212356 14.2360496ORD000009 2011-08-05 00:00:00.000 09 87.6295639 87.6295639 2.7500922 21:00:03.9830000 176.258759 45.001844ORD000010 2009-07-22 00:00:00.000 010 38.523277 38.523277 1.758046 09:05:47.5870000 82.01437 25.16092ORD000011 2009-06-30 00:00:00.000 011 37.0380493 37.0380493 1.7280414 08:44:11.3880000 79.163933 24.560828ORD000012 2008-06-01 00:00:00.000 012 11.05759315 11.05759315 1.2031837 02:26:17.5350000 29.3024515 14.063674ORD000013 2009-12-22 00:00:00.000 013 48.6489178 48.6489178 1.9626044 11:33:04.5100000 101.447418 29.252088ORD000014 2008-06-08 00:00:00.000 014 11.52993403 11.52993403 1.21272594 02:33:09.7600000 30.2089643 14.2545188ORD000015 2011-12-08 00:00:00.000 015 95.8428613 95.8428613 2.9160174 22:59:31.9510000 192.021653 48.320348ORD000016 2008-08-12 00:00:00.000 016 15.79307698 15.79307698 1.29885004 03:35:10.3210000 38.3907538 15.9770008ORD000017 2008-07-29 00:00:00.000 017 14.87196118 14.87196118 1.28024164 03:21:46.4380000 36.6229558 15.6048328ORD000018 2008-07-02 00:00:00.000 018 13.14195499 13.14195499 1.24529202 02:56:36.6150000 33.3027419 14.9058404ORD000019 2008-07-07 00:00:00.000 019 13.46723929 13.46723929 1.25186342 03:01:20.4990000 33.9270249 15.0372684ORD000020 2011-07-31 00:00:00.000 020 87.3081109 87.3081109 2.7435982 20:55:23.4420000 175.641829 44.871964ORD000021 2010-03-29 00:00:00.000 021 55.0457335 55.0457335 2.091833 13:06:07.1850000 113.724135 31.83666ORD000022 2009-07-12 00:00:00.000 022 37.893439 37.893439 1.745322 08:56:37.9100000 80.80559 24.90644ORD000023 2010-11-29 00:00:00.000 023 71.2185715 71.2185715 2.418557 17:01:21.6620000 144.762915 38.37114ORD000024 2010-05-31 00:00:00.000 024 59.1525307 59.1525307 2.1747986 14:05:51.2990000 121.605867 33.495972ORD000025 2011-09-26 00:00:00.000 025 91.0258084 91.0258084 2.8187032 21:49:27.9780000 182.776804 46.374064ORD000026 2010-03-11 00:00:00.000 026 53.8320529 53.8320529 2.0673142 12:48:27.9730000 111.394849 31.346284ORD000027 2008-07-08 00:00:00.000 027 13.52407618 13.52407618 1.25301164 03:02:10.1020000 34.0361058 15.0602328ORD000028 2010-01-15 00:00:00.000 028 50.2297993 50.2297993 1.9945414 11:56:04.1880000 104.481433 29.890828ORD000029 2008-08-23 00:00:00.000 029 16.56943498 16.56943498 1.31453404 03:46:27.8700000 39.8807338 16.2906808ORD000030 2009-12-31 00:00:00.000 030 49.2392251 49.2392251 1.9745298 11:41:39.6870000 102.580331 29.490596ORD000031 2010-09-06 00:00:00.000 031 65.6596423 65.6596423 2.3062554 15:40:30.2330000 134.094263 36.125108ORD000032 2009-10-08 00:00:00.000 032 43.6460122 43.6460122 1.8615356 10:20:18.3370000 91.845882 27.230712ORD000033 2010-11-10 00:00:00.000 033 69.9360166 69.9360166 2.3926468 16:42:42.3410000 142.301446 37.852936ORD000034 2011-10-21 00:00:00.000 034 92.72944 92.72944 2.85312 22:14:14.7840000 186.0464 47.0624ORD000035 2009-10-25 00:00:00.000 035 44.786908 44.786908 1.884584 10:36:54.0280000 94.03548 27.69168ORD000036 2008-11-16 00:00:00.000 036 22.144321 22.144321 1.427158 05:07:33.2250000 50.58001 18.54316ORD000037 2008-06-24 00:00:00.000 037 12.58790248 12.58790248 1.23409904 02:48:33.0780000 32.2394088 14.6819808ORD000038 2008-07-26 00:00:00.000 038 14.67499672 14.67499672 1.27626256 03:18:54.5420000 36.2449432 15.5252512ORD000039 2008-07-18 00:00:00.000 039 14.16076102 14.16076102 1.26587396 03:11:25.7550000 35.2580262 15.3174792ORD000040 2010-12-18 00:00:00.000 040 72.4731292 72.4731292 2.4439016 17:19:36.5490000 147.170652 38.878032ORD000041 2008-10-31 00:00:00.000 041 21.1140775 21.1140775 1.406345 04:52:34.1040000 48.602775 18.1269ORD000042 2008-06-02 00:00:00.000 042 11.13644467 11.13644467 1.20477666 02:27:26.3510000 29.4537827 14.0955332ORD000043 2008-06-16 00:00:00.000 043 12.0751795 12.0751795 1.223741 02:41:05.6110000 31.255395 14.47482ORD000044 2008-08-18 00:00:00.000 044 16.19872453 16.19872453 1.30704494 03:41:04.3410000 39.1692693 16.1408988ORD000045 2010-03-05 00:00:00.000 045 53.4362608 53.4362608 2.0593184 12:42:42.5540000 110.635248 31.186368ORD000046 2009-09-01 00:00:00.000 046 41.2247989 41.2247989 1.8126222 09:45:05.2790000 87.199109 26.252444ORD000047 2008-09-04 00:00:00.000 047 17.31118753 17.31118753 1.32951894 03:57:15.2180000 41.3042993 16.5903788ORD000048 2008-09-29 00:00:00.000 048 18.9815581 18.9815581 1.3632638 04:21:32.9960000 44.510061 17.265276ORD000049 2008-07-23 00:00:00.000 049 14.5061443 14.5061443 1.2728514 03:16:27.1800000 35.920883 15.457028ORD000050 2011-10-19 00:00:00.000 050 92.5561999 92.5561999 2.8496202 22:11:43.5920000 185.713919 46.992404ORD000051 2011-09-12 00:00:00.000 051 90.1021483 90.1021483 2.8000434 21:36:01.8740000 181.004123 46.000868ORD000052 2009-02-07 00:00:00.000 052 27.6273172 27.6273172 1.5379256 06:27:18.3850000 61.102932 20.758512ORD000053 2011-06-24 00:00:00.000 053 84.8381104 84.8381104 2.6936992 20:19:27.8050000 170.901424 43.873984ORD000054 2011-10-05 00:00:00.000 054 91.6650019 91.6650019 2.8316162 21:58:45.8190000 184.003539 46.632324ORD000055 2009-04-16 00:00:00.000 055 32.1208282 32.1208282 1.6287036 07:32:39.9950000 69.726842 22.574072ORD000056 2008-06-20 00:00:00.000 056 12.32097868 12.32097868 1.22870664 02:44:40.1260000 31.7271308 14.5741328ORD000057 2010-05-06 00:00:00.000 057 57.5137342 57.5137342 2.1416916 13:42:01.0770000 118.460702 32.833832ORD000058 2011-10-04 00:00:00.000 058 91.5723775 91.5723775 2.829745 21:57:24.9840000 183.825775 46.5949ORD000059 2008-07-12 00:00:00.000 059 13.79383138 13.79383138 1.25846124 03:06:05.5250000 34.5538178 15.1692248ORD000060 2011-01-20 00:00:00.000 060 74.6220331 74.6220331 2.4873138 17:50:51.9560000 151.294811 39.746276ORD000061 2009-08-22 00:00:00.000 061 40.5782497 40.5782497 1.7995606 09:35:41.0170000 85.958257 25.991212ORD000062 2010-07-24 00:00:00.000 062 62.7707332 62.7707332 2.2478936 14:58:29.0030000 128.549892 34.957872ORD000063 2008-08-15 00:00:00.000 063 15.98402026 15.98402026 1.30270748 03:37:56.9630000 38.7572106 16.0541496ORD000064 2011-01-05 00:00:00.000 064 73.6476751 73.6476751 2.4676298 17:36:41.6070000 149.424831 39.352596ORD000065 2008-07-17 00:00:00.000 065 14.12806825 14.12806825 1.2652135 03:10:57.2230000 35.1952825 15.30427ORD000066 2008-11-06 00:00:00.000 066 21.4632307 21.4632307 1.4133986 04:57:38.8190000 49.272867 18.267972ORD000067 2008-07-18 00:00:00.000 067 14.1571396 14.1571396 1.2658008 03:11:22.5940000 35.251076 15.316016ORD000068 2009-01-22 00:00:00.000 068 26.563384 26.563384 1.516432 06:11:49.8620000 59.06104 20.32864ORD000069 2009-05-07 00:00:00.000 069 33.5306674 33.5306674 1.6571852 07:53:10.4000000 72.432594 23.143704ORD000070 2008-06-25 00:00:00.000 070 12.61874593 12.61874593 1.23472214 02:48:59.9960000 32.2986033 14.6944428ORD000071 2009-05-06 00:00:00.000 071 33.4186489 33.4186489 1.6549222 07:51:32.6390000 72.217609 23.098444ORD000072 2008-07-19 00:00:00.000 072 14.20568722 14.20568722 1.26678156 03:12:04.9630000 35.3442482 15.3356312ORD000073 2010-08-26 00:00:00.000 073 64.9451791 64.9451791 2.2918218 15:30:06.7010000 132.723071 35.836436ORD000074 2008-06-21 00:00:00.000 074 12.36922435 12.36922435 1.2296813 02:45:22.2320000 31.8197235 14.593626ORD000075 2008-06-20 00:00:00.000 075 12.31137865 12.31137865 1.2285127 02:44:31.7480000 31.7087065 14.570254ORD000076 2011-06-27 00:00:00.000 076 85.0724731 85.0724731 2.6984338 20:22:52.3400000 171.351211 43.968676ORD000077 2008-09-06 00:00:00.000 077 17.48660959 17.48660959 1.33306282 03:59:48.3130000 41.6409679 16.6612564ORD000078 2008-11-12 00:00:00.000 078 21.8976625 21.8976625 1.422175 05:03:57.9600000 50.106625 18.4435ORD000079 2008-08-10 00:00:00.000 079 15.67280287 15.67280287 1.29642026 03:33:25.3550000 38.1599247 15.9284052ORD000080 2008-06-08 00:00:00.000 080 11.53970929 11.53970929 1.21292342 02:33:18.2910000 30.2277249 14.2584684ORD000081 2008-06-06 00:00:00.000 081 11.40986485 11.40986485 1.2103003 02:31:24.9720000 29.9785285 14.206006ORD000082 2008-07-23 00:00:00.000 082 14.52555523 14.52555523 1.27324354 03:16:44.1200000 35.9581363 15.4648708ORD000083 2008-06-12 00:00:00.000 083 11.8096714 11.8096714 1.2183772 02:37:13.8950000 30.745834 14.367544ORD000084 2009-06-08 00:00:00.000 084 35.6078359 35.6078359 1.6991482 08:23:23.2020000 76.419079 23.982964ORD000085 2008-10-20 00:00:00.000 085 20.3686768 20.3686768 1.3912864 04:41:43.5720000 47.172208 17.825728ORD000086 2008-08-13 00:00:00.000 086 15.86840014 15.86840014 1.30037172 03:36:16.0580000 38.5353134 16.0074344ORD000087 2008-07-26 00:00:00.000 087 14.69381266 14.69381266 1.27664268 03:19:10.9630000 36.2810546 15.5328536ORD000088 2008-11-24 00:00:00.000 088 22.6560124 22.6560124 1.4374952 05:14:59.7920000 51.562044 18.749904ORD000089 2008-06-28 00:00:00.000 089 12.84987331 12.84987331 1.23939138 02:52:21.7070000 32.7421811 14.7878276ORD000090 2009-01-28 00:00:00.000 090 26.9836786 26.9836786 1.5249228 06:17:56.6640000 59.867666 20.498456ORD000091 2008-07-03 00:00:00.000 091 13.16129167 13.16129167 1.24568266 02:56:53.4900000 33.3398527 14.9136532ORD000092 2010-01-06 00:00:00.000 092 49.5774883 49.5774883 1.9813634 11:46:34.8980000 103.229523 29.627268ORD000093 2008-09-03 00:00:00.000 093 17.25887593 17.25887593 1.32846214 03:56:29.5640000 41.2039033 16.5692428ORD000094 2011-03-18 00:00:00.000 094 78.3632134 78.3632134 2.5628932 18:45:16.9860000 158.474854 41.257864ORD000095 2011-04-18 00:00:00.000 095 80.4599245 80.4599245 2.605251 19:15:46.8430000 162.498845 42.10502ORD000096 2010-02-08 00:00:00.000 096 51.7634578 51.7634578 2.0255244 12:18:22.6540000 107.424818 30.510488ORD000097 2009-06-14 00:00:00.000 097 35.991055 35.991055 1.70689 08:28:57.6480000 77.15455 24.1378ORD000098 2008-08-25 00:00:00.000 098 16.67533528 16.67533528 1.31667344 03:48:00.2920000 40.0839768 16.3334688ORD000099 2008-07-15 00:00:00.000 099 13.94015833 13.94015833 1.26141734 03:08:13.2290000 34.8346473 15.2283468ORD000100 2011-01-03 00:00:00.000 0100 73.517005 73.517005 2.46499 17:34:47.5680000 149.17405 39.2998(100 row(s) affected) Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
wafw1971
Yak Posting Veteran
75 Posts |
Posted - 2013-05-23 : 05:07:32
|
Thanks again Visakh that's did the trick. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-23 : 05:11:06
|
quote: Originally posted by wafw1971 Thanks again Visakh that's did the trick.
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|