|
Hi ,
I am working on some report generation project.Any idea about inserting data from MS SQL SERVER in MS EXCELL Sheet.
G Singh
|
|
|
|
|
This example is using SQL 2005.
Create a new Excel file: c:\testXL.xls
Open the file and write ID into the cell A1, FirstName into the cell B1.
Sace and close the file.
The code below will add 3 rows to the Excel file.
CREATE TABLE #T (ID INT IDENTITY(1,1), FirstName nvarchar(20))
INSERT INTO #T (FirstName) SELECT 'John'
INSERT INTO #T (FirstName) SELECT 'Kyle'
INSERT INTO #T (FirstName) SELECT 'Stacey'
INSERT INTO
OPENROWSET (
'Microsoft.Jet.OLEDB.4.0'
, 'Excel 8.0;Database=c:\testXL.xls;HDR=YES;'
, 'SELECT ID, FirstName FROM [Sheet1$]'
)
SELECT ID, FirstName
FROM #T
DROP TABLE #T
Remember, you have to have the Excel file closed while running the query.
In order to have the code above working PROBABLY you'll have to run also this code to enable *Ad Hoc Distributed Queries* usage.
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
|
|
|
|
|
Thanks leoinfo for providing this infn.
G Singh
|
|
|
|
|
Don't cross post in the forums.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I am using the SQL Server to send an email by executing the msdb.dbo.sp_send_dbmail stored procedure. The problem is that the command is executed but the mail is queued and never sent. That is the email is not being received by the destination. Can someone help me with it please?
Thanks
Regards
|
|
|
|
|
Check that you have a configured profile.
If you pass the profile name as a parameter to the procedure, make sure that the profile exists and is correctly configured.
If you do not pass the profile name, check that you have a public profile which is default or the user executing the command has a private profile.
Check (with Surface Area Configuration) that database mail is enabled. By default, it's disabled
Check that you are a member of DatabaseMailUserRole in msdb.
You can increase logging on mail using command:
EXECUTE msdb.dbo.sysmail_configure_sp 'LoggingLevel' , '2';
This may help to pinpoint the problem.
Information about the queue is retrieved as follows:
sysmail_help_queue_sp @queue_type = 'Mail';
Hope this helps you,
Mika
|
|
|
|
|
Hi i am getting probs in nested cursor...
Outer cursor i am getting managers...,
Inner cursor based on managers getting employee details...,
In this for all the managers the last employee records i am getting more than one time...,
This is my cursor:
DECLARE curMgr CURSOR FOR
SELECT distinct ManagerID FROM @tbl_TempManagers
OPEN curMgr
FETCH NEXT FROM curMgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
WHILE (@@FETCH_STATUS = 0 )
BEGIN
SET @EmpDetail = ''
SELECT @Count=Count(*) FROM @tbl_TempEmployee
--
SET @Counter=1
DECLARE curDetailList CURSOR FOR
SELECT distinct EmployeeName from @tbl_Tempemployee where ManagerId = @mgID order by EmployeeName
OPEN curDetailList
FETCH NEXT FROM curDetailList INTO @EmployeeName1
SELECT dbo.tbl_Login.MID,
convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description],
TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date],
CreationBy as [Name Of Employee] FROM tbl_TSEnterData
INNER JOIN
dbo.tbl_Login ON dbo.tbl_TSEnterData.CreationBy = dbo.tbl_Login.UserName
where CreationBy=@employeename1
AND TSDate BETWEEN
CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101)
order by CreationBy asc,TSDate Desc
WHILE (@@FETCH_STATUS = 0 AND @Counter <= @Count)
BEGIN
SET @EmpDetail = @EmpDetail + @EmployeeName1 + ', '
FETCH NEXT FROM curDetailList INTO @EmployeeName1
SELECT dbo.tbl_Login.MID,
convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description],
TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date],
CreationBy as [Name Of Employee] FROM tbl_TSEnterData
INNER JOIN
dbo.tbl_Login ON dbo.tbl_TSEnterData.CreationBy = dbo.tbl_Login.UserName
where CreationBy=@employeename1
AND TSDate BETWEEN
CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101)
order by CreationBy asc,TSDate Desc
SET @counter=@counter+1
END
CLOSE curDetailList
DEALLOCATE curDetailList
INSERT INTO @Result VALUES (@mgID, @EmpDetail)
FETCH NEXT FROM curmgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
END
CLOSE curmgr
DEALLOCATE curmgr
Actuall o/p have to be like this:
Managerid Date EID EmplName
22 7-2-2008 100 Name1
22 7-3-2008 100 Name1
Managerid Date EID EmplName
22 7-12-2008 101 Name2
22 7-23-2008 101 Name2
Managerid Date EID EmplName
23 7-22-2008 103 Name3
23 7-23-2008 103 Name3
Managerid Date EID EmplName
24 7-12-2008 104 Name4
24 7-30-2008 104 Name4
But,I am getting o/p of that cursor like this:
Managerid Date EID EmplName
22 7-2-2008 100 Name1
22 7-3-2008 100 Name1
Managerid Date EID EmplName
22 7-12-2008 101 Name2
22 7-23-2008 101 Name2
Managerid Date EID EmplName
22 7-12-2008 101 Name2
22 7-23-2008 101 Name2
Managerid Date EID EmplName
22 7-12-2008 101 Name2
22 7-23-2008 101 Name2
Managerid Date EID EmplName
23 7-22-2008 103 Name3
23 7-23-2008 103 Name3
Managerid Date EID EmplName
23 7-22-2008 103 Name3
23 7-23-2008 103 Name3
Managerid Date EID EmplName
23 7-22-2008 103 Name3
23 7-23-2008 103 Name3
Managerid Date EID EmplName
24 7-12-2008 104 Name4
24 7-30-2008 104 Name4
Managerid Date EID EmplName
24 7-12-2008 104 Name4
24 7-30-2008 104 Name4
Managerid Date EID EmplName
24 7-12-2008 104 Name4
24 7-30-2008 104 Name4
See that all the managers last employee record coming more than one time
Becoz of cursor loop i am getting like this...,
How to solve this.......
Thanks & Regards,
NeW OnE,
please don't forget to vote on the post
|
|
|
|
|
One possibile problem is that you are calling FETCH NEXT on your inner cursor at the start of the inner loop. Move the call from the start to the end.
Also, why are you inserting records like
INSERT INTO @Result VALUES (@mgID, @EmpDetail)
When @EmpDetail is a comma separated list of values? There really isn't any reason to do this when you are using a relational database. The relationship between Manager and Employee is maintained when you do this:
CREATE TABLE Manager(
ManagerID INT,
ManagerName VARCHAR(50)
)
CREATE TABLE Employee(
EmployeeID INT,
ManagerID INT,
EmployeeName VARCHAR(50)
)
Then when you want to get data from both tables you do this:
DECLARE @EmployeeID INT
SET @EmployeeID = 5
SELECT EmployeeID,
EmployeeName,
ManagerName
FROM Employee E
INNER JOIN Manager M ON M.ManagerID = E.ManagerID
WHERE EmployeeID = @EmployeeID
When you try and maintain a delimited list in a column you're breaking normalization and it will make the data a nightmare to maintain. If you need to enforce the relationships between tables, create foreign key references and they will prevent you from corrupting the relationships you've defined. If you're going to define your data the way it seems you are, you might as well be using Excel or just plain text files.
|
|
|
|
|
Hi as per our discussion i modified slightly that cursor
Now its creating problem in second manager entry that employee record only binding more than one:
Its modified cursor look at this:
DECLARE curMgr CURSOR FOR
SELECT distinct ManagerID FROM @tbl_TempManagers
OPEN curMgr
FETCH NEXT FROM curMgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
WHILE (@@FETCH_STATUS = 0 )
BEGIN
SET @EmpDetail = ''
SELECT @Count=Count(*) FROM @tbl_TempEmployee
--
SET @Counter=1
DECLARE curDetailList CURSOR FOR
SELECT distinct EmployeeName from @tbl_Tempemployee where ManagerId = @mgID order by EmployeeName
OPEN curDetailList
WHILE (@@FETCH_STATUS = 0 AND @Counter <= @Count)
BEGIN
SELECT dbo.tbl_Login.MID,
convert(varchar(20),TSDate,101) as [Date],TSSubject as [Subject],TSDescription as [Description],
TSHour as [Hour],TSMin as [Minute],convert(varchar(20),CreationDate,101) as [Creation Date],
CreationBy as [Name Of Employee] FROM tbl_TSEnterData
INNER JOIN
dbo.tbl_Login ON dbo.tbl_TSEnterData.CreationBy = dbo.tbl_Login.UserName
where CreationBy=@employeename1
AND TSDate BETWEEN
CONVERT( NVARCHAR, DATEADD(wk, -1, @d) , 101) AND CONVERT( NVARCHAR, DATEADD(dd, -1, @d) , 101)
order by CreationBy asc,TSDate Desc
SET @counter=@counter+1
FETCH NEXT FROM curDetailList INTO @EmployeeName1
END
CLOSE curDetailList
DEALLOCATE curDetailList
FETCH NEXT FROM curmgr INTO @mgID
Insert Into @tbl_TempEmployee Select MId,ID,UserName,EId from tbl_login Where Mid=@mgid
END
CLOSE curmgr
DEALLOCATE curmgr
Thanks & Regards,
NeW OnE,
please don't forget to vote on the post
modified on Friday, July 18, 2008 3:27 AM
|
|
|
|
|
Instead of a cursor, why not try this:
<br />
SELECT <br />
dbo.tbl_Login.MID,<br />
convert(varchar(20),TSDate,101) as [Date],<br />
TSSubject as [Subject],<br />
TSDescription as [Description],<br />
TSHour as [Hour],<br />
TSMin as [Minute],<br />
convert(varchar(20),CreationDate,101) as [Creation Date],<br />
CreationBy as [Name Of Employee] <br />
FROM @tbl_TempManagers M<br />
INNER JOIN tbl_login E ON E.Mid = M.ManagerID<br />
INNER JOIN tbl_TSEnterData D ON D.CreationBy = E.UserName<br />
WHERE D.TSDate BETWEEN DATEADD(wk, -1, @d) <br />
AND DATEADD(dd, -1, @d) <br />
order by D.CreationBy asc,D.TSDate Desc<br />
Temp tables are just like regular tables and can be used in all the same ways. You don't need a foreign key reference to join tables - foreign key references are for data integrity, they aren't required at all in the database, but they are certainly needed to prevent data corruption. So based on what I can tell from your script you can use joins on your temp tables.
Also, as a side note - remove the CONVERT function from your where clause - you're using it to format the date values, but the only reason to format those values is if TSDate is a VARCHAR.
|
|
|
|
|
Hi,
Iam compacting my MSAccess database using the CDaoWorkspace's CompactDatabase function in VC++. Its throwing exception as " Record(s) can't be read; no read permission on DB" . Can anyone let me know the reason for it.
|
|
|
|
|
Sounds like a permission issue. I'd double check that.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi i have two tables
I have to compare this 2 tables...,
Now which dates are not in table1 that date and that ename have to come in select statement
Table1:
TSID TSdate EName
100 7-21-2008 Name1
101 7-21-2008 Name1
102 7-22-2008 Name1
103 7-26-2008 Name1
104 7-23-2008 Name2
105 7-25-2008 Name2
Table2:
Nodates
7-21-2008
7-22-2008
7-23-2008
7-24-2008
7-25-2008
7-26-2008
output:
Date Name
7-23-2008 Name1
7-24-2008 Name1
7-25-2008 Name1
7-21-2008 Name2
7-22-2008 Name2
7-24-2008 Name2
7-26-2008 Name2
how to do that
Thanks & Regards,
NeW OnE,
please don't forget to vote on the post
|
|
|
|
|
Either your output data isn't correct for the example you gave us, or I'm not deciphering your question correctly.
What I think you might want is the Not In syntax:
Select TSDate, EName
From Table1
Where TSDate Not In (Select Nodates From Table2)
|
|
|
|
|
No actually the table 2 data and table 1 data have to come
And see my condition...,
Its problem solved using this query:
select * from (select distinct CreationBy from dbo.tbl_TSEnterData) a cross join @tbl_LastWeekDates b
where not exists (select * from dbo.tbl_TSEnterData where TSdate = b.NoDates and CreationBy = a.CreationBy)
Thanks & Regards,
NeW OnE,
please don't forget to vote on the post
|
|
|
|
|
i want to select both values using union operation how can i do this i use the following sql query
SELECT ProdReqId ,ProdSlNo, convert(varchar(10),ProdCurrDate,105) AS Date1
FROM tbl_ProductRequirementDetails where BrachId=4 and Status=1
UNION ALL
select PurOrderId ,PurOrderCode,convert(varchar(10),PurOrderCurrDate,105) AS Date2 from tbl_PurchaseOrder where BrachId=4 and Status=1
I want to select -- ProdReqId ,ProdSlNo, convert(varchar(10),ProdCurrDate,105) AS Date1 -- and -- PurOrderId ,PurOrderCode,convert(varchar(10),PurOrderCurrDate,105) AS Date2 --
how can i select that
|
|
|
|
|
you realy have to explain in details what do you want to achieve
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
hi
my query =
SELECT * FROM dbo.Categories INNER JOIN dbo.Products ON dbo.Categories.CategoryID = dbo.Products.CategoryID INNER JOIN dbo.Suppliers ON dbo.Products.SupplierID = dbo.Suppliers.SupplierID
and returned column name like ProductID | ProductName | SupplierID | CategoryID
but i want
dbo.Products.ProductID | dbo.Products.ProductName | dbo.Suppliers.SupplierID
how?
thanks
|
|
|
|
|
try this
SELECT Products.ProductID AS 'dbo.Products.ProductID', Products.ProductName AS 'dbo.Products.ProductName', Suppliers.SupplierID AS 'dbo.Suppliers.SupplierID '
FROM dbo.Categories
INNER JOIN dbo.Products
ON dbo.Categories.CategoryID = dbo.Products.CategoryID
INNER JOIN dbo.Suppliers
ON dbo.Products.SupplierID = dbo.Suppliers.SupplierID
you should NEVER EVER SELECT * FROM , even if you do want to get all the columns of a table, specify them.
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
HarveySaayman wrote: you should NEVER EVER SELECT * FROM
Thats an extremely broad statement, can you please elaborate with a reason why!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i dont know the EXACT reason why, but i have been told(by my step mom who's been a data architect at a large insurance company's data warehouse for the past 20 years, and many other people) that SELECT * FROM has significant and unnecessary server overhead. And when your working with db's that have terras and terras of data you can and will bring the system down.
The using of SELECT * FROM worn have a noticeable effect if your working on a much smaller scale db but its considered bad practice.
SQL just has these little things that bring performance down, another example would be using the prefix "sp_" in front of a stored procedure's name... the reason for that is that sp stands for system procedure and NOT stored procedure, so SQL goes and looks through the system procedures first before moving on to stored procedures.
Cheers
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|
|
HarveySaayman wrote: know the EXACT reason why
Yah, I have the same recollection and only use it under special circumstances.
HHmmm, maybe I should talk to your step mum . I think the sp_ issues is well documented and well known but the Select * issues is a little more obscure.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There is nothing difficult with this!
If you need all columns of a table - use *, if you need only a few columns, specify them by their name! And of course you stress the DB Server more by querying all columns of a table instead of selecting only a few of them.
|
|
|
|
|
Hi
on Data base one column has data like this As a representative of Preston Insulation, I
when i show this on my report those styles has to apply for the data .
the data base column can be varchar or xml any data taype.
Thanks in advance
|
|
|
|
|
ch.ramesh wrote: those styles has to apply for the data .
How are you storng these styles? If you look at the code to store the style yu should be able to reverse it back out to apply in your report?
Bob
Ashfield Consultants Ltd
|
|
|
|
|