Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts

Monday, March 19, 2012

Multiple db query call from within different context into #temp table

The first query returns me the results from multiple databases, the
second does the same thing except it puts the result into a #temp
table? Could someone please show me an example of this using the first
query? The first query uses the @.exec_context and I am having a
challenge trying to figure out how to make the call from within a
different context and still insert into a #temp table.

DECLARE @.exec_context varchar(30)
declare @.sql nvarchar(4000)
DECLARE @.DBNAME nvarchar(50)
DECLARE companies_cursor CURSOR FOR
SELECT DBNAME
FROM DBINFO
WHERE DBNAME NOT IN ('master', 'tempdb', 'msdb', 'model')
ORDER BY DBNAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @.DBNAME
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.exec_context = @.DBNAME + '.dbo.sp_executesql '
set @.sql = N'select top 10 * from products'
exec @.exec_context @.sql
FETCH NEXT FROM companies_cursor INTO @.DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
--------------------------
CREATE TABLE #Test (field list here)
declare @.sql nvarchar(4000)
DECLARE @.DBNAME nvarchar(50)

DECLARE companies_cursor CURSOR FOR
SELECT NAME
FROM sysdatabases
WHERE OBJECT_ID(Name+'.dbo.products') IS NOT NULL
ORDER BY NAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @.DBNAME
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.sql = N'select top 10 * from '+@.DBNAME+'.dbo.products'
INSERT INTO #Test
exec (@.sql)
FETCH NEXT FROM companies_cursor INTO @.DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
SELECT * from #Test
DROP TABLE #TestOn Mar 21, 12:57 pm, santaferub...@.gmail.com wrote:

Quote:

Originally Posted by

The first query returns me the results from multiple databases, the
second does the same thing except it puts the result into a #temp
table? Could someone please show me an example of this using the first
query? The first query uses the @.exec_context and I am having a
challenge trying to figure out how to make the call from within a
different context and still insert into a #temp table.
>
DECLARE @.exec_context varchar(30)
declare @.sql nvarchar(4000)
DECLARE @.DBNAME nvarchar(50)
DECLARE companies_cursor CURSOR FOR
SELECT DBNAME
FROM DBINFO
WHERE DBNAME NOT IN ('master', 'tempdb', 'msdb', 'model')
ORDER BY DBNAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @.DBNAME
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.exec_context = @.DBNAME + '.dbo.sp_executesql '
set @.sql = N'select top 10 * from products'
exec @.exec_context @.sql
FETCH NEXT FROM companies_cursor INTO @.DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
-----------------------*----
CREATE TABLE #Test (field list here)
declare @.sql nvarchar(4000)
DECLARE @.DBNAME nvarchar(50)
>
DECLARE companies_cursor CURSOR FOR
SELECT NAME
FROM sysdatabases
WHERE OBJECT_ID(Name+'.dbo.products') IS NOT NULL
ORDER BY NAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @.DBNAME
WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.sql = N'select top 10 * from '+@.DBNAME+'.dbo.products'
INSERT INTO #Test
exec (@.sql)
FETCH NEXT FROM companies_cursor INTO @.DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
SELECT * from #Test
DROP TABLE #Test


Not sure if this solves the problem:

WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.sql = ' INSERT INTO #Test select top 10 * from '+@.DBNAME
+'.dbo.products'
exec (@.sql)
FETCH NEXT FROM companies_cursor INTO @.DBNAME
END

Wednesday, March 7, 2012

Multiple columns?

I have a SPROC that returns any number of vehicle options for a specific
vehicle.
I want to display the information in a multi-column fashion like this...
4speed Auto Windows Floor mats
Radio Bucket Seats Wheels
NOT like this
4speed
Auto Windows
Floor mats
Radio
Bucket Seats
Wheels
What control should I use, and how do I do it? I've tried for a while now
and can't find a solution.
Thanks in advance!
BrianHave you tried using the Table control. In your case, I think you will need
to add a second detail row.
--
Bruce Johnson [MSFT]
Microsoft SQL Server Reporting Services
This posting is provided "AS IS" with no warranties, and confers no rights.
"Brian Cesafsky" <brian.cesafsky@.autotrackerplus.com> wrote in message
news:O0IborWuFHA.2568@.TK2MSFTNGP15.phx.gbl...
>I have a SPROC that returns any number of vehicle options for a specific
>vehicle.
>
> I want to display the information in a multi-column fashion like this...
> 4speed Auto Windows Floor mats
> Radio Bucket Seats Wheels
>
> NOT like this
> 4speed
> Auto Windows
> Floor mats
> Radio
> Bucket Seats
> Wheels
>
> What control should I use, and how do I do it? I've tried for a while now
> and can't find a solution.
>
> Thanks in advance!
> Brian
>
>|||yes, I tried that, but I must be doing something wrong... I just get the
same option repeated... like this...
4speed
4speed
Auto Windows
Auto Windows
Floor mats
Floor mats
Radio
Radio
Bucket Seats
Bucket Seats
Wheels
Wheels
"Bruce Johnson [MSFT]" <brucejoh@.online.microsoft.com> wrote in message
news:%23$aN0fiuFHA.2920@.TK2MSFTNGP10.phx.gbl...
> Have you tried using the Table control. In your case, I think you will
> need to add a second detail row.
> --
> Bruce Johnson [MSFT]
> Microsoft SQL Server Reporting Services
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> "Brian Cesafsky" <brian.cesafsky@.autotrackerplus.com> wrote in message
> news:O0IborWuFHA.2568@.TK2MSFTNGP15.phx.gbl...
>>I have a SPROC that returns any number of vehicle options for a specific
>>vehicle.
>>
>> I want to display the information in a multi-column fashion like this...
>> 4speed Auto Windows Floor mats
>> Radio Bucket Seats Wheels
>>
>> NOT like this
>> 4speed
>> Auto Windows
>> Floor mats
>> Radio
>> Bucket Seats
>> Wheels
>>
>> What control should I use, and how do I do it? I've tried for a while
>> now and can't find a solution.
>>
>> Thanks in advance!
>> Brian
>>
>

Multiple Columns Map to Different Rows in Join Table

Can a SQL statement be written that returns all of the information in
the Project table and the corresponding FullName for both the
CreatedBy and LastModifiedBy fields? Table structures are below.
A typical JOIN clause seems insufficient because every row in the
Project table maps to 2 rows in the User table. When doing reporting,
I've always avoided this problem by using subreports to do individual
lookups. However, I'm curious to see if there is a more direct
approach using SQL only.
Project
- ProjectNo
- ProjectName
- CreatedByUserNo
- LastModifiedByUserNo
User
- UserNo
- FullName
Your input is greatly appreciated.
Best Regards,
DavidSELECT P.projectno, P.projectname,
C.fullname AS createdbyuser,
M.fullname AS lastmodifiedbyuser
FROM Project AS P
JOIN User AS C
ON P.createdbyuserno = C.userno
LEFT JOIN User AS M
ON P.lastmodifiedbyuserno = M.userno
(untested)
--
David Portas
--
Please reply only to the newsgroup
--

Multiple Columns into Single Row -- Very urgent

Hi. I want to return multiple rows into a single row in different columns. For example my query returns something like this

The query looks like this
Select ID, TYPE, VALUE From myTable Where filtercondition = 1

ID TYPE VALUE
1 type1 12
1 type2 15
2 type1 16
2 type2 19

Each ID will have the same number of types and each type for each ID might have a different value. So if there are only two types then each ID will have two types. Now I want to write the query in such a way that it returns

ID TYPE1 TYPE2 VALUE1 VALUE2
1 type1 type2 12 15
2 type1 type2 16 19

Type1, Type2, Value1, and Value2 are all dynamic. Can someone help me please. Thank you.

I've done something like this, but not in SQL. What I do is build a datatable from my select results, populate it, and then return that to my caller. It works something like;

Get Results

Loope through results to get my columns (In my case there may not be a value for every type.)

Build datatable

Loop through results again, populating datatable.

Return datatable

Multiple columns - 3rd post...

I have a SPROC that returns any number of vehicle options for a specific
vehicle.
I want to display the information in a multi-column fashion like this...
4speed Auto Windows Floor mats
Radio Bucket Seats Wheels
NOT like this
4speed
Auto Windows
Floor mats
Radio
Bucket Seats
Wheels
Somone suggested using the Table control and to add a second detail row.
I tried that, but I must be doing something wrong... I just get the
same option repeated... like this...
4speed
4speed
Auto Windows
Auto Windows
Floor mats
Floor mats
Radio
Radio
Bucket Seats
Bucket Seats
Wheels
Wheels
Thanks in advance!Have you tried using the MATRIX feature?
Is there a MAX to how many features a vehicle can have?
"Brian Cesafsky" wrote:
> I have a SPROC that returns any number of vehicle options for a specific
> vehicle.
>
> I want to display the information in a multi-column fashion like this...
> 4speed Auto Windows Floor mats
> Radio Bucket Seats Wheels
>
> NOT like this
> 4speed
> Auto Windows
> Floor mats
> Radio
> Bucket Seats
> Wheels
>
> Somone suggested using the Table control and to add a second detail row.
> I tried that, but I must be doing something wrong... I just get the
> same option repeated... like this...
> 4speed
> 4speed
> Auto Windows
> Auto Windows
> Floor mats
> Floor mats
> Radio
> Radio
> Bucket Seats
> Bucket Seats
> Wheels
> Wheels
> Thanks in advance!
>
>|||sorry, missed that last question... no, there is no MAX
"Scott" <Scott@.discussions.microsoft.com> wrote in message
news:5A0CD99C-62EB-4CCB-AA49-8DE962085186@.microsoft.com...
> Have you tried using the MATRIX feature?
> Is there a MAX to how many features a vehicle can have?
>
> "Brian Cesafsky" wrote:
>> I have a SPROC that returns any number of vehicle options for a specific
>> vehicle.
>>
>> I want to display the information in a multi-column fashion like this...
>> 4speed Auto Windows Floor mats
>> Radio Bucket Seats Wheels
>>
>> NOT like this
>> 4speed
>> Auto Windows
>> Floor mats
>> Radio
>> Bucket Seats
>> Wheels
>>
>> Somone suggested using the Table control and to add a second detail row.
>> I tried that, but I must be doing something wrong... I just get the
>> same option repeated... like this...
>> 4speed
>> 4speed
>> Auto Windows
>> Auto Windows
>> Floor mats
>> Floor mats
>> Radio
>> Radio
>> Bucket Seats
>> Bucket Seats
>> Wheels
>> Wheels
>> Thanks in advance!
>>|||I did try that... didn't work...
"Scott" <Scott@.discussions.microsoft.com> wrote in message
news:5A0CD99C-62EB-4CCB-AA49-8DE962085186@.microsoft.com...
> Have you tried using the MATRIX feature?
> Is there a MAX to how many features a vehicle can have?
>
> "Brian Cesafsky" wrote:
>> I have a SPROC that returns any number of vehicle options for a specific
>> vehicle.
>>
>> I want to display the information in a multi-column fashion like this...
>> 4speed Auto Windows Floor mats
>> Radio Bucket Seats Wheels
>>
>> NOT like this
>> 4speed
>> Auto Windows
>> Floor mats
>> Radio
>> Bucket Seats
>> Wheels
>>
>> Somone suggested using the Table control and to add a second detail row.
>> I tried that, but I must be doing something wrong... I just get the
>> same option repeated... like this...
>> 4speed
>> 4speed
>> Auto Windows
>> Auto Windows
>> Floor mats
>> Floor mats
>> Radio
>> Radio
>> Bucket Seats
>> Bucket Seats
>> Wheels
>> Wheels
>> Thanks in advance!
>>|||Brian,
You could use a list and in the list arrange the textboxes however you
wanted them. You would just need to make sure you had enough boxes for the
vehicle options.
Not sure if this is the correct solution for what you need, but just another
idea.
"Brian Cesafsky" wrote:
> I have a SPROC that returns any number of vehicle options for a specific
> vehicle.
>
> I want to display the information in a multi-column fashion like this...
> 4speed Auto Windows Floor mats
> Radio Bucket Seats Wheels
>
> NOT like this
> 4speed
> Auto Windows
> Floor mats
> Radio
> Bucket Seats
> Wheels
>
> Somone suggested using the Table control and to add a second detail row.
> I tried that, but I must be doing something wrong... I just get the
> same option repeated... like this...
> 4speed
> 4speed
> Auto Windows
> Auto Windows
> Floor mats
> Floor mats
> Radio
> Radio
> Bucket Seats
> Bucket Seats
> Wheels
> Wheels
> Thanks in advance!
>
>|||Create a three column report, with a body so short it only holds the
one text box. Similar approach as would be used for mailing labels.|||I am trying this, but what happens is that I see Column2 and column3 to the
right of the first column, I place the text box in the first column, and
what I see in the out put is the very last option from my query and that is
it.
any ideas?
Thanks again!
"Parker" <psmith@.iquest.net> wrote in message
news:1128084282.571367.232050@.o13g2000cwo.googlegroups.com...
> Create a three column report, with a body so short it only holds the
> one text box. Similar approach as would be used for mailing labels.
>|||Modify your stored proc so that it returns a concatenated string wit
a carriage return after each third entry. Then display the result i
a text box and the text will wrap. The string should something lik
this
item1 & " " & item 2 & " " & item 3 & char(13
& char(10) & item4 ...
Most lilkely you will have to use a cursor to build the string an
also do some text manipulation to force column alignment