I have one stored procedure with one in parameter that returns multiple rows with 3 different columns. I want this result to be inserted inside another table with these 3 column filed and other tables individual fields.
However, when I create the temporary table it shows a blank resultset.
Can anybody help me to sort out this?
CREATE DEFINER=`root`#`localhost` PROCEDURE `getContactRolePermission`(
in contactroleid double
-- inout PermissionTableID double
)
BEGIN
select -- entitydetails.id,
contact.id as userid,
-- contacttyperolemap.contacttypeId ,
contacttyperolemap.roleid ,rolepermission.permissionid
from contacttyperolemap
join rolepermission on contacttyperolemap.roleid=rolepermission.roleid
join entitydetails on entitydetails.id = contacttyperolemap.ContactTypeID
join contact on contact.contactTypeID = entitydetails.id
where contacttyperolemap.contactTypeID = contactroleid
order by contact.id;
CREATE temporary TABLE tmp(
ID double ,
userid double ,
roleid double ,
permissionid double
);
END
stored procedures result is
uid rid pid
2 1 5
2 1 2
2 1 3
2 1 4
2 1 1
23 1 4
23 1 1
23 1 5
23 1 2
23 1 3
26 1 4
26 1 1
26 1 5
26 1 2
26 1 3
I want to insert this data inside userpermission table
schema is
id uid rid pid
1 10 2 1
Looks like you need to create a view instead of a stored procedure for the first 3 rows. Then in a separate query you can query any columns from that view and join that data with whatever other data you want from other tables. Hopefully I read the question right. Heres an article on creating views: https://msdn.microsoft.com/en-us/library/ms187956.aspx
Use INSERT ... SELECT command to fill target table.
And, your temp table shows empty result, because you have not filled it.
Related
I am currently working on a.Net web form solution which generates a brief service report for admins to monitor the services done by technicians.As of now , i am having some trouble in coming up with an efficient SQL (for MySQl) which return data rows along with the missing rows based on the SertvicePrtNum , which is in order.
For Example :-
This is my raw data in the table :-
Id ServiceRptNum Customer_ID Date of Service
---- ------------- ----------- ---------------
1 1001 3 09/10/1997
2 1003 8 10/06/2005
3 1005 1 21/02/2003
4 1007 7 1/06/2011
5 1010 4 4/11/2012
6 1002 2 16/01/2003
Here the ServiceRptNum , 1004 is missing in the table. So i want the db to return the result as : -
Id ServiceRptNum Customer_ID Date of Service
---- ------------- ----------- ---------------
1 1001 3 09/10/1997
2 1002 2 16/01/2003
3 1003 8 10/06/2005
- 1004 - -
4 1005 1 21/02/2003
- 1006 - -
5 1007 7 1/06/2011
- 1008 - -
- 1009 - -
6 1010 4 4/11/2012
Here , the sql additionally generated 1004,1006,1008,1009 since it cannot find those records.
Please note that the Id is automatically generated (auto_increment)while insert of the data.But the Service ReportNum is not , this is to enable the admin to add the service report later on with the manually generated report Num (report num in the hardcopy of the company Servicebook).
You basically need to invent a constant, sequential stream of numbers and then left join your real data to them. For this method to work, you need a table with enough rows in it to generate a counter big enough:
select ID, 1000+n as servicerptnum, customer_id, `Date of Service` from
(
SELECT #curRow := #curRow + 1 AS n
FROM somebigtable
JOIN (SELECT #curRow := 0) r
WHERE #curRow<100
) numbergen
LEFT JOIN
tablewithmissingservicerptnum
ON
servicerptnum = 1000+n
You need to alter some things in the code above because you never told us the name of your table with missing rptnums. You also need to utilise another table in your database with more rows than this table because the way this method works is to count the rows in the bigger table, giving each a number. If you don't have any table bigger than this one, we can probably get enough rows by cross joining a smaller table to itself or by using this table. Replace somebigtable with thistable CROSS JOIN thistable where this table is the name of the table with missing servicerptnums
If you want just the rows that are missing, add a WHERE servicerptnum is null to the end of the sql
Edit, I see you've changed your numbering from:
1001
1002
...
1009
10010
To:
1009
1010
The join condition used to be servicerptnum = concat('100', cast(n as varchar)), it is now servicerptnum = 1000+n..
Look here for ideas on how to generate a group of continuous integers, then select from that left outer join your table. You should get a row for every number but all the values will be null for the missing numbers.
I have some existing mysql tables.
Table 1 - lead_records
leadid leadnumber
1 98264*****
2 88952*****
3 99625*****
and so on.
Table 2 - counsellors
cid counselorid leadnumber
1 101 98264*****
2 102 88952*****
3 101 99625*****
and so on.
Now I want to change some record structure. In place of leadnumber I want to use leadid something like -
cid counselorid leadid
1 101 1
2 102 2
3 101 3
There are thousands of records already stored in that table? How can I replace leadnumber to lead id.
Add counsellors column with index:
ALTER TABLE `counsellors` ADD `leadid` INT NOT NULL, ADD INDEX (`leadid`);
Set leadid based on leadnumber:
UPDATE counsellors c SET c.leadid = (SELECT l.leadid FROM lead_records l WHERE l.leadnumber = c.leadnumber LIMIT 1);
(optional) If everything is ok, drop column:
ALTER TABLE `counsellors` DROP `leadnumber`;
I have a table which stores SQL SELECT statements and stored procedures as followings:
SQLId SQLText
1 IsExistingPlan
2 IsProcessed
The stored procedures designed to return bit data type(true/false).
I have another table, which contains different conditions:
CondId CondGrpId SQLId nFlag
1 1 1 1
2 1 2 0
3 2 1 1
I want to create a temporary table on the basis of the output of executed SQLTexts. The table will have a column "CondVal". The value of the column will be true/false depending on the return value of the procedure true/false.The table structure would be as following:
CondId CondGrpId CondVal nFlag
1 1 1 1
2 1 0 0
3 2 1 1
How I can do the required set of operations effectively by SQL Query?
I am trying to perform this query inside a procedure
set mailSeq = (
select MailSequence
from MailIdentity
where Customer = customer
and MailDate = mailDate
);
i have 4 records in this table
Id Customer MailDate MailSequence
1 1 2014-04-05 00:00:00 5
2 1 2014-04-06 00:00:00 2
3 2 2014-04-05 00:00:00 9
4 2 2014-04-06 00:00:00 5
i call the procedure as
call my_procedure(2, '2014-04-05')
the parameters are customer and mail date
if I perform the query outside the procedure it returns a single record, the one i want, but when i call the procedure it returns the 4 records.
Can any help with this?
I had the same problem and after reading this I realized what it was. Don't have you input parameter be the same name as the fieldname in your where clause.
I also got this problem,and I found that the parameter after where:
like where Customer = customer
and MailDate = mailDate
the left and right side is the same ,so where is true forever
How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4
I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.