Showing posts with label runtime. Show all posts
Showing posts with label runtime. Show all posts

Thursday, March 22, 2012

Creating a Stored Procedure that will summarize data in a table into a table reflecting period d

I am attempting to create a stored procedure that will launch at report runtime to summarize data in a table into a table that will reflect period data using an array type field. I know how to execute one line but I am not sure how to run the script so that it not only summarizes the data below but also creates and drops the table.

Any help would be greatly appreciated.

Current Table

Project | Task | Category | Fiscal Year | Fiscal Month | Total Hours

Proj 1 | Task 1 | Cat 1 | 2007 | 01 | 40

Proj 1 | Task 1 | Cat 2 | 2007 | 02 | 20

Proj 1 | Task 1 | Cat 3 | 2007 | 03 | 35

Proj 1 | Task 1 | Cat 1 | 2008 | 01 | 40

Proj 1 | Task 1 | Cat 2 | 2008 | 02 | 40

Proj 1 | Task 1 | Cat 3 | 2008 | 03 | 40

Proposed Table

Project | Task | Category | Fiscal Month 01 | Fiscal Month 02 | Fiscal Month 03 | Fiscal Year

Proj 1 | Task 1 | Cat 1 | 40 | 0 | 0 | 2007

Proj 1 | Task 1 | Cat 2 | 0 | 20 | 0 | 2007

Proj 1 | Task 1 | Cat 3 | 0 | 0 | 35 | 2007

Proj 1 | Task 1 | Cat 1 | 40 | 0 | 0 | 2008

Proj 1 | Task 1 | Cat 2 | 0 | 40 | 0 | 2008

Proj 1 | Task 1 | Cat 3 | 0 | 0 | 40 | 2008

Thanks,

Mike Misera

Check out the PIVOT operator

|||

Mike, check out this sample script. You would need a case statement for each month, this is just using one for each month supplied in the example.

Code Snippet

CREATE TABLE #currentTable (

Project NVARCHAR(10),

Task NVARcHAR(10),

Category NVARcHAR(10),

FiscalYear INT,

FiscalMonth INT,

TotalHours INT

)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 1', 2007, 01, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 2', 2007, 02, 20)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 3', 2007, 03, 35)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 1', 2008, 01, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 2', 2008, 02, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 3', 2008, 03, 40)

SELECT Project, Task, Category, FiscalYear,

SUM(CASE FiscalMonth WHEN 1 THEN TotalHours ELSE 0 END) AS '01',

SUM(CASE FiscalMonth WHEN 2 THEN TotalHours ELSE 0 END) AS '02',

SUM(CASE FiscalMonth WHEN 3 THEN TotalHours ELSE 0 END) AS '03'

FROM #currentTable

GROUP BY Project, FiscalYear, Task, Caetegory

DROP TABLE #currentTable

|||

ShawnNSF,

When I run this statement the results in query analyzer it pulls back the results that I want to see on the bottom of the results pane based off of the the Selection statement looking into X_PJLABDIS(view).

My problem with this script is the INSERT step. It adds the values in the parantheses and I need it to add the values returned from the SELECT Statement below that.

Mike

CREATE TABLE PIVOT (

Project NVARCHAR(10),
pjt_entity NVARcHAR(10),
acct NVARcHAR(10),
Fiscalno_year INT,
Fiscalno_month INT,
total_hrs INT

)

INSERT INTO PIVOT VALUES ('Project', 'Pjt_entity', 'acct', 2007, 01, 40)

SELECT Project, pjt_entity, acct, Fiscalno_year,

SUM(CASE Fiscalno_month WHEN 1 THEN total_hrs ELSE 0 END) AS 'Month 01',
SUM(CASE Fiscalno_month WHEN 2 THEN total_hrs ELSE 0 END) AS 'Month 02',
SUM(CASE Fiscalno_month WHEN 3 THEN total_hrs ELSE 0 END) AS 'Month 03',
SUM(CASE Fiscalno_month WHEN 4 THEN total_hrs ELSE 0 END) AS 'Month 04',
SUM(CASE Fiscalno_month WHEN 5 THEN total_hrs ELSE 0 END) AS 'Month 05',
SUM(CASE Fiscalno_month WHEN 6 THEN total_hrs ELSE 0 END) AS 'Month 06',
SUM(CASE Fiscalno_month WHEN 7 THEN total_hrs ELSE 0 END) AS 'Month 07',
SUM(CASE Fiscalno_month WHEN 8 THEN total_hrs ELSE 0 END) AS 'Month 08',
SUM(CASE Fiscalno_month WHEN 9 THEN total_hrs ELSE 0 END) AS 'Month 09',
SUM(CASE Fiscalno_month WHEN 10 THEN total_hrs ELSE 0 END) AS 'Month 10',
SUM(CASE Fiscalno_month WHEN 11 THEN total_hrs ELSE 0 END) AS 'Month 11',
SUM(CASE Fiscalno_month WHEN 12 THEN total_hrs ELSE 0 END) AS 'Month 12'

FROM X_PJLABDIS

GROUP BY Project, Fiscalno_year, pjt_entity, acct

|||

Mike,

Those values are just for example. Do not use that part of the script. You seem to have gotten the idea by modifying the select statement (the part that was for you). I should have explained a little better that the other part of my snippet was just me building test data to show you the example query.

You can just run a query with the same idea as what I posted against the table you have with the data already populated in it.

|||

Shawn,

As you can probably tell right now, I am have very limited knowledge of and using pivot tables and stored procedures. As you said I am going down the right path with this code.

How would I go about achieving returning the results into a report or table? Need all the help I can get right now and you are helping a lot and very much appreciate it.

Thanks,

Mike

|||

FYI, using pivot with SS 2005

Code Snippet

Create Table #Project (

Project nvarchar(10),

Task nvarchar(10),

Category nvarchar(10),

FisicalYear int,

FisicalMonth int,

totalhrs Int

);

--Sample Data

Insert Into #Project Values('Project 1','Task 1', 'Cat 1', '2007', '1', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 2', '2007', '2', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 3', '2007', '3', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 4', '2007', '4', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 5', '2007', '5', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 6', '2007', '6', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 7', '2007', '7', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 8', '2007', '8', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 9', '2007', '9', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 10', '2007', '10', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 11', '2007', '11', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 12', '2007', '12', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 1', '2008', '01', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 2', '2008', '02', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 3', '2008', '03', '40' );

--Pivot Query

SELECT Project,Task, Category, FisicalYear,

ISNULL([1],0) AS 'Month 01',ISNULL([2],0) AS 'Month 02',ISNULL([3],0) AS 'Month 03',ISNULL([4],0) AS 'Month 04',

ISNULL([5],0) AS 'Month 05',ISNULL([6],0) AS 'Month 06',ISNULL([7],0) AS 'Month 07',ISNULL([8],0) AS 'Month 08',

ISNULL([9],0) AS 'Month 09',ISNULL([10],0) AS 'Month 10',ISNULL([11],0) AS 'Month 11',ISNULL([12],0) AS 'Month 12'

FROM (SELECT Project, Task, Category, FisicalYear,FisicalMonth,totalhrs,

Row_NUmber() OVER (partition by FisicalYear Order by FisicalMonth) as RowNum

FROM #Project) p

PIVOT

(SUM(totalhrs) FOR FisicalMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))

AS pvt

ORDER BY FisicalYear, RowNum

--

--SELECT Project,Task, Category, FisicalYear,

--SUM(CASE FisicalMonth WHEN 1 THEN totalhrs ELSE 0 END) AS 'Month 01',

--SUM(CASE FisicalMonth WHEN 2 THEN totalhrs ELSE 0 END) AS 'Month 02',

--SUM(CASE FisicalMonth WHEN 3 THEN totalhrs ELSE 0 END) AS 'Month 03',

--SUM(CASE FisicalMonth WHEN 4 THEN totalhrs ELSE 0 END) AS 'Month 04',

--SUM(CASE FisicalMonth WHEN 5 THEN totalhrs ELSE 0 END) AS 'Month 05',

--SUM(CASE FisicalMonth WHEN 6 THEN totalhrs ELSE 0 END) AS 'Month 06',

--SUM(CASE FisicalMonth WHEN 7 THEN totalhrs ELSE 0 END) AS 'Month 07',

--SUM(CASE FisicalMonth WHEN 8 THEN totalhrs ELSE 0 END) AS 'Month 08',

--SUM(CASE FisicalMonth WHEN 9 THEN totalhrs ELSE 0 END) AS 'Month 09',

--SUM(CASE FisicalMonth WHEN 10 THEN totalhrs ELSE 0 END) AS 'Month 10',

--SUM(CASE FisicalMonth WHEN 11 THEN totalhrs ELSE 0 END) AS 'Month 11',

--SUM(CASE FisicalMonth WHEN 12 THEN totalhrs ELSE 0 END) AS 'Month 12'

--

--FROM #Project

--

--GROUP BY Project,Task, Category, FisicalYear

--cleanup

drop table #Project

Creating a Stored Procedure that will summarize data in a table into a table reflecting period d

I am attempting to create a stored procedure that will launch at report runtime to summarize data in a table into a table that will reflect period data using an array type field. I know how to execute one line but I am not sure how to run the script so that it not only summarizes the data below but also creates and drops the table.

Any help would be greatly appreciated.

Current Table

Project | Task | Category | Fiscal Year | Fiscal Month | Total Hours

Proj 1 | Task 1 | Cat 1 | 2007 | 01 | 40

Proj 1 | Task 1 | Cat 2 | 2007 | 02 | 20

Proj 1 | Task 1 | Cat 3 | 2007 | 03 | 35

Proj 1 | Task 1 | Cat 1 | 2008 | 01 | 40

Proj 1 | Task 1 | Cat 2 | 2008 | 02 | 40

Proj 1 | Task 1 | Cat 3 | 2008 | 03 | 40

Proposed Table

Project | Task | Category | Fiscal Month 01 | Fiscal Month 02 | Fiscal Month 03 | Fiscal Year

Proj 1 | Task 1 | Cat 1 | 40 | 0 | 0 | 2007

Proj 1 | Task 1 | Cat 2 | 0 | 20 | 0 | 2007

Proj 1 | Task 1 | Cat 3 | 0 | 0 | 35 | 2007

Proj 1 | Task 1 | Cat 1 | 40 | 0 | 0 | 2008

Proj 1 | Task 1 | Cat 2 | 0 | 40 | 0 | 2008

Proj 1 | Task 1 | Cat 3 | 0 | 0 | 40 | 2008

Thanks,

Mike Misera

Check out the PIVOT operator

|||

Mike, check out this sample script. You would need a case statement for each month, this is just using one for each month supplied in the example.

Code Snippet

CREATE TABLE #currentTable (

Project NVARCHAR(10),

Task NVARcHAR(10),

Category NVARcHAR(10),

FiscalYear INT,

FiscalMonth INT,

TotalHours INT

)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 1', 2007, 01, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 2', 2007, 02, 20)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 3', 2007, 03, 35)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 1', 2008, 01, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 2', 2008, 02, 40)

INSERT INTO #currentTable VALUES ('Proj 1', 'Task 1', 'Cat 3', 2008, 03, 40)

SELECT Project, Task, Category, FiscalYear,

SUM(CASE FiscalMonth WHEN 1 THEN TotalHours ELSE 0 END) AS '01',

SUM(CASE FiscalMonth WHEN 2 THEN TotalHours ELSE 0 END) AS '02',

SUM(CASE FiscalMonth WHEN 3 THEN TotalHours ELSE 0 END) AS '03'

FROM #currentTable

GROUP BY Project, FiscalYear, Task, Caetegory

DROP TABLE #currentTable

|||

ShawnNSF,

When I run this statement the results in query analyzer it pulls back the results that I want to see on the bottom of the results pane based off of the the Selection statement looking into X_PJLABDIS(view).

My problem with this script is the INSERT step. It adds the values in the parantheses and I need it to add the values returned from the SELECT Statement below that.

Mike

CREATE TABLE PIVOT (

Project NVARCHAR(10),
pjt_entity NVARcHAR(10),
acct NVARcHAR(10),
Fiscalno_year INT,
Fiscalno_month INT,
total_hrs INT

)

INSERT INTO PIVOT VALUES ('Project', 'Pjt_entity', 'acct', 2007, 01, 40)

SELECT Project, pjt_entity, acct, Fiscalno_year,

SUM(CASE Fiscalno_month WHEN 1 THEN total_hrs ELSE 0 END) AS 'Month 01',
SUM(CASE Fiscalno_month WHEN 2 THEN total_hrs ELSE 0 END) AS 'Month 02',
SUM(CASE Fiscalno_month WHEN 3 THEN total_hrs ELSE 0 END) AS 'Month 03',
SUM(CASE Fiscalno_month WHEN 4 THEN total_hrs ELSE 0 END) AS 'Month 04',
SUM(CASE Fiscalno_month WHEN 5 THEN total_hrs ELSE 0 END) AS 'Month 05',
SUM(CASE Fiscalno_month WHEN 6 THEN total_hrs ELSE 0 END) AS 'Month 06',
SUM(CASE Fiscalno_month WHEN 7 THEN total_hrs ELSE 0 END) AS 'Month 07',
SUM(CASE Fiscalno_month WHEN 8 THEN total_hrs ELSE 0 END) AS 'Month 08',
SUM(CASE Fiscalno_month WHEN 9 THEN total_hrs ELSE 0 END) AS 'Month 09',
SUM(CASE Fiscalno_month WHEN 10 THEN total_hrs ELSE 0 END) AS 'Month 10',
SUM(CASE Fiscalno_month WHEN 11 THEN total_hrs ELSE 0 END) AS 'Month 11',
SUM(CASE Fiscalno_month WHEN 12 THEN total_hrs ELSE 0 END) AS 'Month 12'

FROM X_PJLABDIS

GROUP BY Project, Fiscalno_year, pjt_entity, acct

|||

Mike,

Those values are just for example. Do not use that part of the script. You seem to have gotten the idea by modifying the select statement (the part that was for you). I should have explained a little better that the other part of my snippet was just me building test data to show you the example query.

You can just run a query with the same idea as what I posted against the table you have with the data already populated in it.

|||

Shawn,

As you can probably tell right now, I am have very limited knowledge of and using pivot tables and stored procedures. As you said I am going down the right path with this code.

How would I go about achieving returning the results into a report or table? Need all the help I can get right now and you are helping a lot and very much appreciate it.

Thanks,

Mike

|||

FYI, using pivot with SS 2005

Code Snippet

Create Table #Project (

Project nvarchar(10),

Task nvarchar(10),

Category nvarchar(10),

FisicalYear int,

FisicalMonth int,

totalhrs Int

);

--Sample Data

Insert Into #Project Values('Project 1','Task 1', 'Cat 1', '2007', '1', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 2', '2007', '2', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 3', '2007', '3', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 4', '2007', '4', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 5', '2007', '5', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 6', '2007', '6', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 7', '2007', '7', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 8', '2007', '8', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 9', '2007', '9', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 10', '2007', '10', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 11', '2007', '11', '20' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 12', '2007', '12', '35' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 1', '2008', '01', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 2', '2008', '02', '40' );

Insert Into #Project Values('Project 1','Task 1', 'Cat 3', '2008', '03', '40' );

--Pivot Query

SELECT Project,Task, Category, FisicalYear,

ISNULL([1],0) AS 'Month 01',ISNULL([2],0) AS 'Month 02',ISNULL([3],0) AS 'Month 03',ISNULL([4],0) AS 'Month 04',

ISNULL([5],0) AS 'Month 05',ISNULL([6],0) AS 'Month 06',ISNULL([7],0) AS 'Month 07',ISNULL([8],0) AS 'Month 08',

ISNULL([9],0) AS 'Month 09',ISNULL([10],0) AS 'Month 10',ISNULL([11],0) AS 'Month 11',ISNULL([12],0) AS 'Month 12'

FROM (SELECT Project, Task, Category, FisicalYear,FisicalMonth,totalhrs,

Row_NUmber() OVER (partition by FisicalYear Order by FisicalMonth) as RowNum

FROM #Project) p

PIVOT

(SUM(totalhrs) FOR FisicalMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))

AS pvt

ORDER BY FisicalYear, RowNum

--

--SELECT Project,Task, Category, FisicalYear,

--SUM(CASE FisicalMonth WHEN 1 THEN totalhrs ELSE 0 END) AS 'Month 01',

--SUM(CASE FisicalMonth WHEN 2 THEN totalhrs ELSE 0 END) AS 'Month 02',

--SUM(CASE FisicalMonth WHEN 3 THEN totalhrs ELSE 0 END) AS 'Month 03',

--SUM(CASE FisicalMonth WHEN 4 THEN totalhrs ELSE 0 END) AS 'Month 04',

--SUM(CASE FisicalMonth WHEN 5 THEN totalhrs ELSE 0 END) AS 'Month 05',

--SUM(CASE FisicalMonth WHEN 6 THEN totalhrs ELSE 0 END) AS 'Month 06',

--SUM(CASE FisicalMonth WHEN 7 THEN totalhrs ELSE 0 END) AS 'Month 07',

--SUM(CASE FisicalMonth WHEN 8 THEN totalhrs ELSE 0 END) AS 'Month 08',

--SUM(CASE FisicalMonth WHEN 9 THEN totalhrs ELSE 0 END) AS 'Month 09',

--SUM(CASE FisicalMonth WHEN 10 THEN totalhrs ELSE 0 END) AS 'Month 10',

--SUM(CASE FisicalMonth WHEN 11 THEN totalhrs ELSE 0 END) AS 'Month 11',

--SUM(CASE FisicalMonth WHEN 12 THEN totalhrs ELSE 0 END) AS 'Month 12'

--

--FROM #Project

--

--GROUP BY Project,Task, Category, FisicalYear

--cleanup

drop table #Project

Wednesday, March 7, 2012

Creating a formula at run time

Is it possible to create a formula at runtime. I want to add many formulas at runtime without pre-defining them.
Thanks a lotWhy do you want to do this?|||hi Madhi,
Tks 4 ur concern.
Im building a Report Tool, it should facilitate any number of columns (it should be very flexible). So I plan to use formulas, using them I can pass parameters to say what to display on the report. If I can create new formulas at run time this is possible.
Regards,
Janitha|||I dont know whether this helps you.
Add the columns in a tablebase table add design that report using those fields|||Madhi,
I'm designing a report tool. it will be used to create many user defined customizable reports. I can use known number of formulas (say 10) as report fields and pass and DB field or calculation to the report. But my problem is if I use 10 (or even 100) formulas, I have limited the number of maximum fields the report can display. To avoid that limitation I want to add formulas at run time.
Anyway I dont understand what you mean by tablebase table. Please help me out.
Thanks
rgds
Janitha|||Do u think this is possible|||Janitha,
Does your Report have 10 fields as default?
Anyway I dont understand what you mean by tablebase table.
I mean database table|||hi Madhi!
Thanks again,
Im using 10 blank formulas. I pass queries at run time to those formulas to generate any report. But I dont want any limitations, well I can think of using 100 fields. Nobody never will user 100 fields in one report, isnt it. But Im looking for a more professional solutions.|||Janitha,
I think the only way is add as many fields as possible that CR allows and pass the values to them
If you use Crystal Report Viewer, then there is an option to add formulas at runtime

CrRpt.FormulaFields.Add "FormulaName", "Value"|||I need VB.NET code for Report Designer.|||can any one help me please!|||Do any one know a better way?
:wave:|||need VB.NET code for Report Designer

Janitha,
Do you need VB.NET code to call the Report?|||No no, I want to create formulas using VB.NET code at run time, thanks for ur earlier reply. But I must use report designer, it provide much flexible way to design the report, I only couldnt find this option.
Thanks|||It seems this is impossible @.##@.|||Yea!!! It seems this is impossible @.##@.|||It seems this is impossible @.##@.|||Janitha,
Search for your solution in this web site
http://support.businessobjects.com/|||Well Madhi! I tried "businessobjects" site as well. but I could not get their tech support cos I dont have a Licence. They don't give sulutions otherwise.

The only option is to use "Crystal Repository"

Thanks 4 ur concern Madhi

Creating a dynamic temporary table

I need to dynamically create a temporary table like this (I don't know its
structure until runtime - it is based on selections made by user):
DECLARE @.CreateStatement = 'CREATE TABLE #tmpTable (' + @.co1 + 'varchar(250)
+ ', ' + @.col2 + 'varchar2(250))'
EXEC(@.CreateStatement)
--following is code to fill this table
The table created is not accessible after the line EXEC(@.CreateStatement). I
know this (temp table have a scope limited to the stored procedure that
created them).
Is there another way to accomplish this? I also tried using table variables,
but I wasn't able to make a stored procedure that returns a table variable.YOU CAN USE FUNCTION INSTEAD OF STORED PROC
CREATE FUNCTION TEMP (@.col1 varchar(250) ,@.col2 varchar(250))
RETURNS TABLE
as
RETURN SELECT @.col1+','+@.col2 as TEXT
"razdanro" wrote:
> I need to dynamically create a temporary table like this (I don't know its
> structure until runtime - it is based on selections made by user):
> DECLARE @.CreateStatement = 'CREATE TABLE #tmpTable (' + @.co1 + 'varchar(25
0)
> + ', ' + @.col2 + 'varchar2(250))'
> EXEC(@.CreateStatement)
> --following is code to fill this table
> The table created is not accessible after the line EXEC(@.CreateStatement).
I
> know this (temp table have a scope limited to the stored procedure that
> created them).
> Is there another way to accomplish this? I also tried using table variable
s,
> but I wasn't able to make a stored procedure that returns a table variable.[/color
]|||You can create tempdb..temptable or create ##temptable and it will be
available until it is dropped or the sql server is re-booted.. The
difference between #temptable and ##temptable
#temptable is a non-sharable connection specific temporary table. It goes
away when the SP (if created in an sp) or connection goes away..
##temptable can be seen by all spids, and lives until you drop it or the
server is re-booted... if you are wiriting for mutliple concurrnet users,
you may have to check for its existence before creating it... or come up
with some unque name, and/or attach a spid to separate your rows from those
inserted by another spid.
hope this helps.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"razdanro" <razdanro@.discussions.microsoft.com> wrote in message
news:8F211B2B-42BE-4608-98EB-C4E350D64D8C@.microsoft.com...
> I need to dynamically create a temporary table like this (I don't know its
> structure until runtime - it is based on selections made by user):
> DECLARE @.CreateStatement = 'CREATE TABLE #tmpTable (' + @.co1 +
'varchar(250)
> + ', ' + @.col2 + 'varchar2(250))'
> EXEC(@.CreateStatement)
> --following is code to fill this table
> The table created is not accessible after the line EXEC(@.CreateStatement).
I
> know this (temp table have a scope limited to the stored procedure that
> created them).
> Is there another way to accomplish this? I also tried using table
variables,
> but I wasn't able to make a stored procedure that returns a table
variable.|||create the table first using a hard coded "create table" statement (with at
least one column - a dummy column if needed) - then dynamically alter its
structure.
"razdanro" <razdanro@.discussions.microsoft.com> wrote in message
news:8F211B2B-42BE-4608-98EB-C4E350D64D8C@.microsoft.com...
> I need to dynamically create a temporary table like this (I don't know its
> structure until runtime - it is based on selections made by user):
> DECLARE @.CreateStatement = 'CREATE TABLE #tmpTable (' + @.co1 +
'varchar(250)
> + ', ' + @.col2 + 'varchar2(250))'
> EXEC(@.CreateStatement)
> --following is code to fill this table
> The table created is not accessible after the line EXEC(@.CreateStatement).
I
> know this (temp table have a scope limited to the stored procedure that
> created them).
> Is there another way to accomplish this? I also tried using table
variables,
> but I wasn't able to make a stored procedure that returns a table
variable.|||Do not write SQL this way.
Temporary tables tell us that you are really writing procedural code
and have not learned to think in sets and declarative code yet. A temp
table is a "scratch tape" for an algorithm based on procedural steps in
95% of the cases. You probably should be using derived tables or
VIEWs.
Dynamic SQL tell us that you do not know what you are doing, so you
have to let a random stranger create a table in your data model at the
last minute.
Using over-sized VARCHAR(n) values tells us that you did no research to
find the proper size, but just grabbed a large dummy value. This also
means that you have no data model and probalby no data dictionary.
Finally, you will never learn SQL this way. You have already decided
on HOW you want to solve a problem. So people will show you how to
write kludges for your bad solution. But if you had posted WHAT you
want to do, then you might get a relational answer.
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.
Let's try again with the actual problem.|||Here is the data model that I didn't design, but I have to work with right n
ow.
I have three entities: Companies, Sites and Contacts. A Company has 0 or
more Sites, a Site has 0 or more Contacts.
The application must allow users to add properties of these entities
dynamically.
These Properties are held in a table, and the values allowed are in a
PropertyValues table. There is also a table EntityProperty which is an
intersection table between Entities and PropertyValues.
So I have a design that actually stores data and metadata.
And now I have to make a SQLBuilder based on these Entities. I don't know
what Property will be selected as output, that's why I need to create a
dynamic temporary table to return the result.