2012年3月8日星期四
Any SQL Gods out there
History:
3 Tables: Tooldb - Employee - ToolUserdb
Scenario:
I have a webform in c# that gathers data concerning internal tools(applications) that are written in-house. One of the fields is a listbox of names pulled from the Employee table called Creator. When the form is submitted I need to have the list of selected employees published to the ToolUserdb.
My SP:
ALTER PROCEDURE dbo.InsertTool
(
@.ToolName nvarchar(250),
@.Platform nvarchar(250),
@.Vendor nvarchar(250),
@.Subplatform nvarchar(250),
@.Submitter nvarchar(250),
@.Finders nvarchar(250),
@.LTD nvarchar(50),
@.JobArea nvarchar(250),
@.Func nvarchar(250),
@.Owners nvarchar(250),
@.Active nvarchar(250),
@.Version numeric,
@.Build numeric,
@.CWSTD nvarchar(50),
@.Status nvarchar(250),
@.Cost numeric,
@.Notes nvarchar(250),
@.Keywords nvarchar(250),
@.Links nvarchar(250),
@.Paths nvarchar(250),
@.eid int,
@.tdbid int,
@.Creator nvarchar(250)
)
AS
INSERT INTO [ToolDB] (ToolName, Platform, Vendor, Subplatform, Submitter, Finders, LTD, JobArea, Func, Owners, Active, Version, Build, CWSTD, Status, Cost, Notes, Keywords, Links, Paths)
VALUES
(@.ToolName, @.Platform, @.Vendor, @.Subplatform, @.Submitter, @.Finders, @.LTD, @.JobArea, @.Func, @.Owners, @.Active, @.Version, @.Build, @.CWSTD, @.Status, @.Cost, @.Notes, @.Keywords, @.Links, @.Paths)
INSERT INTO ToolUsersdb (@.Creator) SELECT + @.eid + ',[ID] FROM Employee + @.tdbid + ',[ID] FROM ToolDB IN (@.Creator)
Error:
Incorrect syntax near keywork IN (referring to last insert statement).
Can someone tell me how I can get this multi-insert stmt. to work?
Thank you!
TimWhat's the DDL for ToolUsersDB table?
INSERT INTO ToolUsersdb (EmpID, ToolID, Creator) values (@.eid, @.tdbid, @.Creator) ?|||if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ToolUsersdb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ToolUsersdb]
GO
CREATE TABLE [dbo].[ToolUsersdb] (
[id] [int] NOT NULL ,
[eid] [int] NULL ,
[tdbid] [int] NULL
) ON [PRIMARY]
GO|||After examining my SP closer I have made the following changes. I think this is closer to the end result. The problem is that I am getting an error:
The select list for the insert statement contains fewer items than the insert list. Not sure why this is.
ALTER PROCEDURE dbo.InsertTool
(
@.ToolName nvarchar(250),
@.Platform nvarchar(250),
@.Vendor nvarchar(250),
@.Subplatform nvarchar(250),
@.Submitter nvarchar(250),
@.Finders nvarchar(250),
@.LTD nvarchar(50),
@.JobArea nvarchar(250),
@.Func nvarchar(250),
@.Owners nvarchar(250),
@.Active nvarchar(250),
@.Version numeric,
@.Build numeric,
@.CWSTD nvarchar(50),
@.Status nvarchar(250),
@.Cost numeric,
@.Notes nvarchar(250),
@.Keywords nvarchar(250),
@.Links nvarchar(250),
@.Paths nvarchar(250),
@.eid int,
@.tdbid int,
@.Creator nvarchar(250)
)
AS
INSERT INTO [ToolDB] (ToolName, Platform, Vendor, Subplatform, Submitter, Finders, LTD, JobArea, Func, Owners, Active, Version, Build, CWSTD, Status, Cost, Notes, Keywords, Links, Paths)
VALUES
(@.ToolName, @.Platform, @.Vendor, @.Subplatform, @.Submitter, @.Finders, @.LTD, @.JobArea, @.Func, @.Owners, @.Active, @.Version, @.Build, @.CWSTD, @.Status, @.Cost, @.Notes, @.Keywords, @.Links, @.Paths)
INSERT INTO [ToolUsersdb] (eid, tdbid) select e.id from employee e where e.id in (@.Creator) select t.id from Tooldb t|||Huh?
INSERT INTO [ToolUsersdb] (eid, tdbid)
select e.id from employee e where e.id in (@.Creator) select t.id from Tooldb t
What this?
You can't do that|||Brett,
Could you expand on your thoughts?
Do you have any ideas on how this can be done. I am sure that I am not the first person to be presented with a one to many realtionship between tables.
Even a link would be helpful if you know of one.
Thanks,
Tim|||It looks like, and correct me if I'm wrong, and INSERT statement with 2 SELECTs...
I guess you could do it this way...
INSERT INTO [ToolUsersdb] (eid, tdbid)
SELECT e.id, t.id
FROM employee e JOIN Tooldb t
ON e.id = t.id
WHERE e.id = @.Creator
The IN error message is erroneous...|||Brett,
Thanks for helping out. You thoughts are appreciated.
Here is the modification I have made (with your thoughts in mind):
INSERT INTO [ToolUsersdb] (eid, tdbid)
SELECT e.id FROM employee e WHERE e.id = (@.Creator)
UNION
SELECT t.id FROM Tooldb t
I put the UNION in place because my understanding is that without it, the second SLELECT statement would be ignored.
With the above script I am getting the following error:
The Select list for the insert statment contains fewer items than the Insert list.
There are only two columns in the ToolUsersdb outside of the ID column.
Any additional ideas?
Have I explained what I am trying to do clearly enough in the beginning? Let me know if you need any further clarification.
Thanks again.
Tim|||Concepts!
You're mixing rows with columns.
For example, your INSERT expects 2 columns per row.
Your result set is producing 1 column per row.
If you want e.id and t.id on the same row, you have to relate them somehow.
2012年2月11日星期六
Answer: My First Report
department
employee
employee
and so on.....
department
employee
employee
and so on.....
department
My data is stored in two tables
department -
deptID
deptName
Main -
deptID
empName
title
order by
Departments are listed in department table. there is an associated deptID in both tables. in the main table every employee is listed and they have a deptID that is associated with the department table. The order by column is an integer that list the order they should appear in the format. 1 would be the top person and so on.
How would you bring in the data into reporting services to display in this format? Would this be one SQL Statement or am I looking at multiple Datasets?
I was playing with the Union ALL and realized I don't have equal parts.
I will be pulling the deptid and deptname from the department tables
from the main table I need deptid to match tables, and then I need to display. name, title, and phone. department table is just used to display the department name.
|||Do let me know if the following is what you were looking to accomplish:I created the following 2 tables (similar to yours I believe):
create table department (deptID int, deptName varchar(100))
create table Main (deptID int, empName varchar(100), title varchar(100))
When designing the report:
Enter the following in the Query String (or the query that you have to select all the required fields from the tables) -
select * from department
join main on main.deptid = department.deptid
Click Next, Choose Tabular Matrix
Click Next, Choose DeptName as 'Group' and EmpName and Title as 'Details'
Click Next, select 'Enable drilldown' if you wanted to expand the deptnames
Enter report name, click Finish and view the Preview.
You will find the report grouped by Dept Name, with empname, title appearing under each dept name.