Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Patyk
Yak Posting Veteran
74 Posts |
Posted - 2015-03-17 : 12:33:22
|
I have the following code to create a view. Instead of having a view I would like to convert this code to create a store procedure so the data is saved in a table.ThanksSELECT TOP (100) PERCENT StockCode, MAX(categ) AS categ, MAX(flavor) AS flavor, MAX(Type) AS TypeFROM (SELECT RTRIM(KeyField) AS StockCode, RTRIM(AlphaValue) AS categ, RTRIM(AlphaValue) AS flavor, RTRIM(AlphaValue) AS Type, DateValueFROM companyB.dbo.AdmFormData WHERE (0 = 1)UNION ALLSELECT RTRIM(KeyField) AS StockCode, RTRIM(AlphaValue) AS categ, NULL AS flavor, NULL AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_3WHERE (FieldName = 'categ')UNION ALLSELECT RTRIM(KeyField) AS StockCode, NULL AS categ, RTRIM(AlphaValue) AS flavor, NULL AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_2WHERE (FieldName = 'flavor')UNION ALLSELECT RTRIM(KeyField) AS StockCode, NULL AS categ, NULL AS flavor, RTRIM(AlphaValue) AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_1WHERE (FieldName = 'type')) AS aGROUP BY StockCode, DateValueORDER BY StockCode, DateValue |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-03-17 : 12:43:27
|
insert into table_to_save_data (...your columns...)select ...your columns...from yourview |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-03-17 : 12:47:49
|
[code]Create Procedure mytableinsertasBeginInsert into mytable -- table you want to insert into, include column names (column1, column2) etcSELECT TOP (100) PERCENT StockCode, MAX(categ) AS categ, MAX(flavor) AS flavor, MAX(Type) AS TypeFROM (SELECT RTRIM(KeyField) AS StockCode, RTRIM(AlphaValue) AS categ, RTRIM(AlphaValue) AS flavor, RTRIM(AlphaValue) AS Type, DateValueFROM companyB.dbo.AdmFormData WHERE (0 = 1)UNION ALLSELECT RTRIM(KeyField) AS StockCode, RTRIM(AlphaValue) AS categ, NULL AS flavor, NULL AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_3WHERE (FieldName = 'categ')UNION ALLSELECT RTRIM(KeyField) AS StockCode, NULL AS categ, RTRIM(AlphaValue) AS flavor, NULL AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_2WHERE (FieldName = 'flavor')UNION ALLSELECT RTRIM(KeyField) AS StockCode, NULL AS categ, NULL AS flavor, RTRIM(AlphaValue) AS Type, DateValueFROM companyB.dbo.AdmFormData AS AdmFormData_1WHERE (FieldName = 'type')) AS aGROUP BY StockCode, DateValueORDER BY StockCode, DateValueEnd-- to run exec mytableinsertGO[/code]We are the creators of our own reality! |
|
|
|
|
|
|
|