Hey guys I'm trying figure out if there is a way to insert a result from a stored procedure into a table without going through ASP.NET. This is just an experiment and for learning purposes since i did manage to work it in .NET.
For example.
CREATE PROCEDURE dbo.inserttoLog
@Modem_ID VARCHAR(7)
As
Select dbo.PRODUCT.MODEM_ID + ' ' + dbo.PRODUCT.COLOUR + dbo.PRODUCT.SPEED AS Part#
FROM PRODUCT
WHERE ([Part#] LIKE '%' + @Modem_ID + '%')
I want to take the concatenated result of PART# and insert it into a log table. Is there a way of doing that?
1 john john smith smith 1 john j smith smith 1 john jon smith smit
I want to be able to insert all the records from the result set into another table, like in the eg, but only excluding those ones that match all the above fields. How would I do this...
Hello Friends My problem is Suppose Query is - Select * from tbl_Employee TBL_EMPLOYEE HAS ONLY TWO COLUMNS NAME,POST I need that an extra column get inserted in result through query showing serial Number with each row.So that query result look like this. Serial Name Post 1 XYZ QER 2 SDF OPO 3 WER IPO If any body knows please post the solution its urgent.
---------------------------------------------------------------------- I executed it in my SQL Server Management Studio Express and I got: Commands completed successfully. I do not know where the result is and how to get the result viewed. Please help and advise.
I am trying to insert data into two different tables. I will insert into Table 2 based on an id I get from the Select Statement from Table1. Insert Table1(Title,Description,Link,Whatever)Values(@title,@description,@link,@Whatever)Select WhateverID from Table1 Where Description = @DescriptionInsert into Table2(CategoryID,WhateverID)Values(@CategoryID,@WhateverID) This statement is not working. What should I do? Should I use a stored procedure?? I am writing in C#. Can someone please help!!
I have created a table Table with name as Varchar and id as int. Now i have started inserting the rows like, insert into Table values ('arun',20).Yes i have inserted a row in the table. Now i have got the values " arun's ", 50. insert into Table values('arun's',20) My sqlserver is giving me an error instead of inserting the row. How will you solve this problem?
I am trying to insert bulk data into main table from staging table in sql server 2012. If any error comes, this total activity is rollbacked. I don't want that to happen. I want to know the records where ever the problem persists, and the rest has to be inserted.
But now I am getting error "General Network Error. Check your network documentation" after specifying Use existing stored procedure in TableAdpater Configuration Wizard.
ALTER PROCEDURE dbo.Insert_MailSignature( @Singnature image )
AS
SET NOCOUNT OFF;
INSERT INTO MailsSignature (Singnature) VALUES (@Singnature);
SELECT Id, Singnature FROM MailsSignature WHERE (Id = SCOPE_IDENTITY())
For testing I created a desktop application and found that the same Code, same(Use existing stored procedure in TableAdpater Configuration Wizard) and same stored procedure is working fine in inserting image into the table.
Hi, I have a table in which I will insert several redundant data. Don't ask why, is Integration services, it only reads data and inserts it in a SQL table. THis way, I have a SQL table with several lines repeating them selves. What I want to do is create a procedure that reads the distinct data and inserts it in another table, but my problem is that I am not able to select data line by line on the original table to save it in local variables and insert it on the another table, I just can select the last line. I've tried a while cycle but no succeed. Here is my code: create proc insertLocalizationASdeclare @idAp int, @macAp varchar(20), @floorAp varchar(2), @building varchar(30), @department varchar(30)select @idAp = idAp from OLTPLocalization where idAp not in (select idAp from dimLocalization)select @macAp=macAp,@floorAp=floorAp,@building=building,@department=department from OLTPLocalizationif (@idAp <> null)beginInsert into dimLocalization VALUES(@idAp,@macAp,@floorAp,@building,@department)endGO This only inserts the last line in the "oltpLocalization" table. O the other hand, like this:create proc aaaaasdeclare @idAp as int, @macAp as varchar(50), @floorAp as int, @building as varchar(50), @department as varchar(50)while exists (select distinct(idAp) from OLTPLocalization)begin select @idAp =idAp from OLTPLocalization where idAp not in (select idAp from dimLocalization) select @macAp = macAp from OLTPLocalization where idAp = @idAp select @building = building from OLTPLocalization where idAp = @idAp select @department = department from OLTPLocalization where idAP = @idApif (@idAp <> null)begin insert into dimLocalization values(@idAp,@macAp,@floorAp,@building,@department)endendgo this retrieves every distinct idAp in each increment on the while statement. The interess of the while is really selecting each different line in the OLTPLocalization table. I did not find any foreach or for each statement, is there any way to select distinct line by line in a sql table and save each column result in variables, to then insert them in another table? I've also thought about web service, that reads the distinct data from the oltpLocalization into a dataset, and then inserts this data into the dimLocalization table. Is there anything I can do?Any guess?Really needing a hand here!Thanks a lot!
I write a insert trigger on my table LeaveRegister(1000 rows) and inserting rows in audit table, but when i inserting a row in LeaveRegister table. In audit table 1000 + 1 rows are inserting every time.
I have two table with some identical fields and I am trying to populate one of the tables with a row that has been selected from the other table.Is there some standard code that I can use to take the selected row and input the data into the appropriate fields in the other table?
I need to return a table of values calculated from other tables. I have about 10 reports which will use approx. 6 different table structures.
Would it be better performance wise to create a physical table in the database to update while calculating using an identity field to id the stored procedure call, return the data and delete the records. For Example:
I have a 2010 SSIS package where I am reading csv files with different fields and formatted data, I have created separate packages for each file and I am formatting the data to fit the final destination tables data elements, I've been instructed to create 7 separate packages to read each file and the format the data from the 7 csv files, and insert into their table tbl1, tbl2, tbl3...etc then, I'm taking a execute sql task and wanting to insert the tbl1, tbl2, tbl3...etc into destination table that will be the final table for all reports and other uses.
1- should I create a ID? 2- these files will be read once a month 3- I want to append the data, not drop and recreate each run, 4- It's 2012 SQL and 2010 SSIS
Each csv file is in a different format, some have 15 columns, other have 8 I have to parse the data, in SP to align with the fields in the destination table.
5- Can I force RowID to be the next auto gen number from tbl1,for the start of insert for tbl2, then last row of tbl2 for insert of tbl3???
I'm working on a database for a financial client and part of what i need to do is calculate a value from two separate rows in the same table and insert the result in the same table as a new row. I have a way of doing so but i consider it to be extremely inelegant and i'm hoping there's a better way of doing it. A description of the existing database schema (which i have control over) will help in explaining the problem:
id metric_id metric_type_id metric_name 1 80 2 Fiscal Enterprise Value Historic Year 1 2 81 2 Fiscal Enterprise Value Current Fiscal Year 3 82 2 Fiscal Enterprise value Forward Fiscal year 1 4 83 2 Fiscal Enterprise Value Forward Fiscal Year 2 5 101 3 Calendar Enterprise value Historic Year 1 6 102 3 Calendar Enterprise Value Current Fiscal Year 5 103 3 Calendar Enterprise value Forward Year 1 6 104 3 Calendar Enterprise Value Forward Year 2
Table Name: metric_type_details
id metric_type_id metric_type_name 1 1 Raw 2 2 Fiscal 3 3 Calendar 4 4 Calculated
The problem scenario is the following: Because a certain number of the securities have a fiscal year end that is different to the calendar end in addition to having fiscal data (such as fiscal enterprise value and fiscal earnings etc...) for each security i also need to store calendarised data. What this means is that if security with security_id = 3 has a fiscal year end of October then using rows with ids = 1, 2, 3 and 4 from the metrics_ladder table i need to calculate metrics with metric_id = 83, 84, 85 and 86 (as described in the metric_details table) and insert the following 4 new records into metrics_ladder:
Metric with metric_id = 101 (Calendar Enterprise value Historic Year 1) will be calculated by taking 10/12 of the value for metric_id 80 plus 2/12 of the value for metric_id 81.
Similarly, metric_id 102 will be equal to 10/12 of the value for metric_id 81 plus 2/12 of the value for metric_id 82,
metric_id 103 will be equal to 10/12 of the value for metric_id 82 plus 2/12 of the value for metric_id 83 and finally
metric_id 104 will be NULL (determined by business requirements as there is no data for forward year 3 to use).
As i could think of no better way of doing this (and hence the reason for this thread) I am currently achieving this by pivoting the relevant data from the metrics_ladder so that the required data for each security is in one row, storing the result in a new column then unpivoting again to store the result in the metrics_ladder table. So the above data in nmetrics_ladder becomes:
-- Dummy year variable to make it easier to use MONTH() function -- to convert 3 letter month to number. i.e. JAN -> 1, DEC -> 12 etc... DECLARE @DUMMY_YEAR VARCHAR(4) SET @DUMMY_YEAR = 1900;
with temp(security_id, metric_id, value) as ( select ml.security_id, ml.metric_id, ml.value from metrics_ladder ml where ml.metric_id in (80,81,82,83,84,85,86,87,88,etc...) -- only consider securities with fiscal year end not equal to december and ml.security_id in (select security_id from company_details where fiscal_year_end <> 'dec') ) insert into @calendar_averages select temppivot.security_id -- Net Income ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[80]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[81]) as [101] ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[81]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[82]) as [102] ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[82]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[83]) as [103] ,NULL as [104] -- Share Holders Equity ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[84]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[85]) as [105] ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[85]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[86]) as [106] ,(CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR))/12*[86]) +((12 - CONVERT(DECIMAL, MONTH(cd.fiscal_year_end + @DUMMY_YEAR)))/12*[87]) as [107] ,NULL as [108] -- Capex -- Sales -- Accounts payable etc... .. .. from temp pivot ( sum(value) for metric_id in ([80],[81],[82],[83],[84],[85],[86],[87],[88],etc...) ) as temppivot inner join company_details cd on temppivot.security_id = cd.security_id
********* END SQL *********
The result then needs to be unpivoted and stored in metrics_ladder.
And FINALLY, the question! Is there a more elegant way of achieving this??? I have complete control over the database schema so if creating mapping tables or anything along those lines would help it is possible. Also, is SQL not really suited for such operations and would it therefore be better done in C#/VB.NET.
I got one table with 3 columns = Column1, Column2, Column3
Sample Table
Column1 | Column2 | Column3 ------------------------------------ A | 12 | 0 A | 13 | 2 B | 12 | 5 C | 5 | 0
Select Column1, Column2, Column3 as New1 Where Column1 = A AND Column2 = 12 AND Column3 = 0
Select Column1, Column2, Column3 as New2 Where Column1 = A AND Column2 = 12 AND Column3 >0
The only difference is one condition Column3 = 0 and another one Column3 > 0. This two condition is not an "AND" condition... but just two separate information need to be display in one table. So how do i display the result in one table where the new Output will be in this manner
I am looking for an alternate logic for below-mentioned code where I am inserting into a table and having left join with the same table
insert TABLE1([ID],[Key],[Return]) select distinct a.[ID],cat1,cat2 from (select ID,[Key] Cat1 ,[Return] cat2 from @temp as temp) a left join TABLE1 oon a.ID= o.ID and a.Cat1 = o.[Key] and a.cat2 = o.[return] where [key] is null order by ID
Hey all, Quick question for ya. If I wanted to insert 1 value into 1 table and another value into another table, how would I do that with closing connections and reopening and whatnot? I am using Visual Web Dev and SQL server 2005 express. I have been trying to mess around with it but I can't figure it out. Here is the situation. I want to insert a GroupName and GroupDescription that a user fills in in 2 text boxes named GroupNametxt and GroupDescriptiontxt. This data will go into the "Group" table. I then also want to Insert the data UserID (taken from the aspnet ID table), and GroupName from the Grouptxt.text into a GroupMembership table. How would I do all this in 1 button click? I know some basic TSQL but I don't know how to handle the opening and closing of connections and whatnot. Thanks, Chris
VWD 2005 Express. Visual Basic. Sql Server 2005. How may I insert a record into a table from code behind (I am doing this without any entries to a form). The connection name is GoodNews_Extranet. The table is Login. The fields I want to write are UserId and dtTime. Thanks for any help. The insert command is: INSERT INTO [Login] ([SystemUserId], [dtTime]) VALUES (@SystemUserId, @dtTime) I will provide the @ parameters programmatically. I just need to know the commands I should use in Visual Basic to actually execute the INSERT command. Thanks for the help.
I'm passing that string to an insert stored procedure that hits a varchar field (varchar 5000).
problem is, nothing is showing up in the database. there are no errors, but the field I have shows nothign in it after the insert.
if i run a test and set my string to "this is a test" it goes into the table from the SP_insert no problem.
So i'm figuring, somehow the formatting of the XML string in C# isn't kosher with MS SQL.
the above text is the exact string value from VS.NET command output (? mystring).
I'm really in need of help here guys. I'm trying to log this XML result into a log table for reference if there are ever any problems I can look in the log table @ the xml.
I have the 3 tables below. I want to be able to add more than one item to a quote. I thought I had designed the tables so that I could have more than one item in a quote....? I've tried to do this:
insert into has_quote(date_of_quote,service_desk_contact,category,cat_ref) values('09/12/2002', 'me',2,'C92') where quote_id = '1'
but it doesn't work. Am I totally missing something here?
I have created a CTE to combine some data. I have also created a temporary table that I need this data that the CTE returns to be inserted to and then everything that is in this temp table needs to be inserted to an existing table in my database. I don't know how to select the values from the CTE into the temp table?
Insert Into #TempLabTestConfigImport (labtestkey,valuecode,value_description,value_type,units,table_name,field_name)
Does my select go here??? I tried select * and it doesn't like that syntax...
With labtestcheck (labtestcode) as ( select a.labtestcode from EMR_temp_labtest_configupdate a Where Not Exists (Select * From lab_test_add_conf b where b.labtest_key = a.labtestcode) )
select row_number() over (partition by t.labtestcode order by b.valuecode) as count, t.labtestcode,b.zsegcode,b.valuecode,b.valuedesc,b.valuetype,b.units,b.tablename,b.fieldname from EMR_temp_labtest_configupdate a inner join emr_zseg_code_descriptions b on a.zsegcode = b.zsegcode inner join labtestcheck t on t.labtestcode = a.labtestcode
I am using MSSQL and I want to do something like this:
insert into people values (newid(), *value from names table* )
but I can't work out how to select the value from one table and insert it into another table in a single query. Using a subquery or combining with a select has not worked.
Is this a job for a datacursor or is there a simpler way?
I am trying to insert data into a table from another where not exists. This will be a table that writes from one table to another if there has been an update from the original table. Both tables have the same field and table names. This is what I have so far:
USE cana_01imis
DECLARE @Gen_Tables.Code AS VARCHAR(60) DECLARE @Gen_Tables.DESCRIPTION AS VARCHAR(255)
DECLARE cur CURSOR FAST_FORWARD FOR SELECT Gen_Tables.Code, Gen_Tables.DESCRIPTION FROM dbo.cana_01imis.gen_tables WHERE NOT IN (SELECT Gen_Tables.Code FROM Gen_Tables)
FETCH NEXT FROM cur INTO @Gen_Tables.Code, @Gen_Tables.DESCRIPTION WHILE @@FETCH_STATUS = 0 BEGIN --SELECT Gen_Tables.Code, Gen_Tables.DESCRIPTION from gen_tables
DECLARE @DNNGen_Tables AS VARCHAR(60) SET @DNNGen_Tables.code = @Gen_Tables.Code
DECLARE @DNNGen_Tables.Code DESCRIPTION AS VARCHAR(255) SET @DNNGen_Tables.Code DESCRIPTION = @Gen_Tables.description
-- then your insert statement goes here INSERT INTO cana_01dnn.gen_tables VALUES (Gen_Tables.Code,Gen_Tables.DESCRIPTION)
FETCH NEXT FROM cur INTO @Gen_Tables.Code,@Gen_Tables.Code END
I have a pb when i transfer data from a table named INCIDENT to a table GI_INCIDENTS.... In the table INCIDENT i have startdate,enddate,starttime,endtime And in the table GI_INCIDENTS i have startdate and enddate -->format yyyy/MM/dd hh:mm:ss.ttt.. INCIDENT is a migrated table from access...Then with a query i transfered datas to GI_INCIDENTS... The pb is in INCIDENT Table, date of beginning incident is (2003/06/18 ) but when i execute my insert query,in the table GI_INCIDENTS, date of beginning incident is (2003/06/06)... So i have 2 days delay in the all colums... INCIDENT-->enddate (2006/11/30) GI_INCIDENT-->enddate(2006/11/28) I don't understand the fact...
The query:
Insert into GI_INCIDENTS(GIIN_ID,GIIN_STA_INCIDENT,STARTDATE,ENDDATE,GIIN_TYPE,GIIN_RESUME,GIIN_DESCRIPTION,GIIN_IMPACT_ANTENNE,GIIN_INITIATEUR) select NUMINCIDENT,CODETAT,STARTDATE+ ' ' + STARTTIME, ENDDATE + ' ' + ENDTIME,CODETYPE,NATURE,DESINCIDENT,CODBLOQ,NUMEXPL From INCIDENT set IDENTITY_INSERT GI_INCIDENTS off
I have a problem to which I'm not finding a solution for, and I thought maybe some of you could give me a few hints on how to approach it.
I need to make available a SP. For each row returned by the SP, I need to make an insert on a 2nd table, using one of the parameters used on the SP, the PK of the original table and some more info. As in to identify rows, with a specific execution of a particular SP.
im not very good at sql but need to query the database to use in myprogramming script.if the database is just like thisid name parent_id1 A null <----- root2 B 13 C 14 D 15 E 26 F 27 G 38 H 49 I 410 J 411 K 9the data in the above table is just like some sort of tree datawhich have parent and child nodeand now,how to query into the result like thisthe deepest node is in level 4what i want is how deep this tree data is?anyone show me the query script or store procedure to find theresult please?many thanks in advance,--Posted via http://dbforums.com
I want to see all the product line provided they are there in the table or not, i.e.
Product Line Product Line Description Total Usage Quantity
10000 Raw Material 0
20000 Intermediate Product 0
30000 Bearing 2713321
32000 High Strength 4258197
34000 High Temp 0
36000 Corrosion Resistant 639492
50000 High Speed 1452153
52000 Die Steel Hot Work 1614727
54000 Die Steel Cold Work 464943
88000 Misc 0
90000 Conversion 0 This is the result i am looking out for now the problem is that those record are not there in table so naturally they will not be shown in the result. Can any one help me modifying below query? so that i can get the aforesaid result
Code Block
SELECT [PRODL] as 'Product Line' , (case when prodl = 10000 then 'Raw Material' when prodl = 20000 then 'Intermediate Product' when prodl = 30000 then 'Bearing' when prodl = 32000 then 'High Strength' when prodl = 34000 then 'High Temp' when prodl = 36000 then 'Corrosion Resistant' when prodl = 50000 then 'High Speed' when prodl = 52000 then 'Die Steel Hot Work' when prodl = 54000 then 'Die Steel Cold Work' when prodl = 88000 then 'Misc' when prodl = 90000 then 'Conversion' end) as 'Product Line Description'
,sum(case when [PRODL] = 10000 then [Usage Qty] when [PRODL] = 20000 then [Usage Qty] when [PRODL] = 30000 then [Usage Qty] when [PRODL] = 32000 then [Usage Qty] when [PRODL] = 34000 then [Usage Qty] when [PRODL] = 36000 then [Usage Qty] when [PRODL] = 50000 then [Usage Qty] when [PRODL] = 52000 then [Usage Qty] when [PRODL] = 54000 then [Usage Qty] when [PRODL] = 88000 then [Usage Qty] when [PRODL] = 90000 then [Usage Qty] end) as 'Total Usage Quantity'
FROM [LATCUBDAT].[dbo].[RMU] group by prodl order by prodl
Result of The Aforesaid Query
Product Line Product Line Description Total Usage Quantity