Showing posts with label model. Show all posts
Showing posts with label model. Show all posts

Tuesday, March 20, 2012

Creating table in tempdb using user-defined type

I understand that to create a temp table in tempdb using a user-defined
type, the udt must be defined in Model. However, this doesn't work in
my situation, due to security limitations being placed on the Model
database. (I am using SQL Server 2000).
I'm wondering if there is any viable workaround, short of of using
fixed datatypes when creating the temp table.
For example, say I want to create a temp table as follows:
CREATE TABLE #MyTable(
MyColumn ut_MyColumnType
)
As noted, the above will not work unless ut_MyColumnType is defined in
Model, which can't be done in my situation.
Since I know that ut_MyColumnType is Varchar, I tried using the
COL_LENGTH function to retrieve the size from a column in a non-temp
table that I know is defined using ut_MyColumn type (is there some way
to get the size directly from ut_MyColumnType') into the variable
@.MyColumnLength, and then doing:
CREATE TABLE #MyTable(
MyColumn Varchar(@.MyColumnLength)
)
But this unfortunately doesn't work (Error 170: Incorrect syntax near
'@.MyColumnLength').
Is there any reasonable way to create a temp table using either a
user-defined type not defined in Model, or some substitute method as I
was trying above'IraG wrote:
> I understand that to create a temp table in tempdb using a
> user-defined type, the udt must be defined in Model. However, this
> doesn't work in my situation, due to security limitations being
> placed on the Model database. (I am using SQL Server 2000).
> I'm wondering if there is any viable workaround, short of of using
> fixed datatypes when creating the temp table.
> For example, say I want to create a temp table as follows:
> CREATE TABLE #MyTable(
> MyColumn ut_MyColumnType
> )
> As noted, the above will not work unless ut_MyColumnType is defined in
> Model, which can't be done in my situation.
> Since I know that ut_MyColumnType is Varchar, I tried using the
> COL_LENGTH function to retrieve the size from a column in a non-temp
> table that I know is defined using ut_MyColumn type (is there some way
> to get the size directly from ut_MyColumnType') into the variable
> @.MyColumnLength, and then doing:
> CREATE TABLE #MyTable(
> MyColumn Varchar(@.MyColumnLength)
> )
> But this unfortunately doesn't work (Error 170: Incorrect syntax near
> '@.MyColumnLength').
> Is there any reasonable way to create a temp table using either a
> user-defined type not defined in Model, or some substitute method as I
> was trying above'
You need to use dynamic sql in order to extract the data type and length
of the udt. That's easy enough. The problem is that you are creating a
temp table and because you need to use dynamic sql, the scope of the
temp table is only valid inside the dynamic sql call. Once that call
returns, the temp table is gone. If you were creating a permanent table,
you could do it.
For example, the following fails on the select of the temp table:
exec sp_addtype 'test_type', 'varchar(123)', 'NOT NULL'
go
exec sp_help 'test_type'
create proc dbo.create_temp_table
as
Begin
create table #udt (
[name] varchar(255),
[type] varchar(255),
[length] int,
[precision] int,
[scale] int,
[nullable] varchar(3),
[default_name] varchar(255),
[rule_name] varchar(255),
[collation] varchar(255) )
insert into #udt
exec sp_help 'test_type'
declare @.type varchar(255)
declare @.length int
declare @.new_table nvarchar(1000)
Select TOP 1
@.type = [type],
@.length = [length]
From #UDT
Set @.new_table = 'Create Table #test_table (my_col ' + @.type + '(' +
CAST(@.length as VARCHAR(4)) + '))'
Print @.new_table
Exec (@.new_table)
Select * from #test_table -- fails
Drop table #udt
End
go
exec dbo.create_temp_table
go
exec sp_droptype 'test_type'
go
David Gugick
Imceda Software
www.imceda.com|||Can you create a stored procedure which creates your udt's and mark the proc
as startup
(sp_procoption)?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"IraG" <ijgla@.clas.ucsb.edu> wrote in message
news:1116459324.112987.289060@.g47g2000cwa.googlegroups.com...
>I understand that to create a temp table in tempdb using a user-defined
> type, the udt must be defined in Model. However, this doesn't work in
> my situation, due to security limitations being placed on the Model
> database. (I am using SQL Server 2000).
> I'm wondering if there is any viable workaround, short of of using
> fixed datatypes when creating the temp table.
> For example, say I want to create a temp table as follows:
> CREATE TABLE #MyTable(
> MyColumn ut_MyColumnType
> )
> As noted, the above will not work unless ut_MyColumnType is defined in
> Model, which can't be done in my situation.
> Since I know that ut_MyColumnType is Varchar, I tried using the
> COL_LENGTH function to retrieve the size from a column in a non-temp
> table that I know is defined using ut_MyColumn type (is there some way
> to get the size directly from ut_MyColumnType') into the variable
> @.MyColumnLength, and then doing:
> CREATE TABLE #MyTable(
> MyColumn Varchar(@.MyColumnLength)
> )
> But this unfortunately doesn't work (Error 170: Incorrect syntax near
> '@.MyColumnLength').
> Is there any reasonable way to create a temp table using either a
> user-defined type not defined in Model, or some substitute method as I
> was trying above'
>|||If you only care about the type, precision and scale and don't want to
preserve rules or defaults in the temp table then you could do it from
an existing column in the current database:
EXEC sp_addtype x_type, 'INTEGER', 'NOT NULL'
GO
CREATE TABLE foo (col x_type)
/* Create a temp table */
SELECT col
INTO #foo
FROM foo
WHERE 1=0
SELECT * FROM #foo
DROP TABLE foo
EXEC sp_droptype x_type
User-defined types are virtually obsolete. In my view there is no good
reason to use them and they come with quite a few divantages. Books
Online recommends using CHECK constraints and DEFAULT constraints
instead and I would suggest you follow that advice if you can. Those
alternatives are more powerful and flexible.
David Portas
SQL Server MVP
--|||David,
Thanks for the great suggestion (SELECT col INTO #foo FROM foo WHERE
1=0). (I would mention that I actually thought of the same thing lying
in bed last night, but I know that nobody would believe me even though
it's true..)
Regarding UDTs being virtually obsolete: where exactly does BOL
recommend using CHECKs and DEFAULTs instead? I'm not clear on how using
these constraints can be used to insure uniformity when defining
columns in different tables.
I realize that UDTs can be a pain when they need to be redefined, but
at least they give you something to get a handle on in that
circumstance. And there is always the hope that future releases of SQL
Server will be more supportive of UDT redefinition.
Would anyone know if SQL Server 2005 offers any better support for UDT
redefinition?|||http://msdn.microsoft.com/library/d...br />
20qb.asp
"Rules are a backward-compatibility feature that perform some of the
same functions as CHECK constraints. CHECK constraints are the
preferred, standard way to restrict the values in a column. CHECK
constraints are also more concise than rules; there can only be one
rule applied to a column, but multiple CHECK constraints can be
applied. CHECK constraints are specified as part of the CREATE TABLE
statement, while rules are created as separate objects and then bound
to the column."
Similar story for Defaults. IMO the advantages of CHECK and DEFAULT
constraints far outweigh the tiny benefit of giving a shorthand name to
a datatype. A data dictionary, good change control procedures and a
quick cut-and-paste between CREATE TABLE statements are a surer way to
guarantee consistency of data elements.
Technically, although Rules and Defaults are deprecated I don't think
Types are (yet) but AFAIK there is no new functionality around them in
2005. Yukon has a new model for UDTs using the CLR.
David Portas
SQL Server MVP
--

Sunday, March 11, 2012

Creating schemabound views on linked servers

Hi,
I need to create indexes and use index hints on views created
across two servers (in the linked server model). Assuming that it is
possible to do so, I first need to create an unique clustered index on
the view. This in turn mandates that the underlying view is a
schemabound view that takes only a two part name (dbo.objectname).
However, a view created on a linked server has four parts in its name
(linkedserver_name,db_name,owner_nameobj
ect_name). This causes the sql
parser to reject my request to create a schema bound linked server
view. Can anybody tell me if there is a work around for this?
Thanks
/SudhaAn indexed view cannot store data from a table that resides in another datab
ase or another server...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<srajagop@.genesyslab.com> wrote in message
news:1163034549.484430.255030@.i42g2000cwa.googlegroups.com...
> Hi,
> I need to create indexes and use index hints on views created
> across two servers (in the linked server model). Assuming that it is
> possible to do so, I first need to create an unique clustered index on
> the view. This in turn mandates that the underlying view is a
> schemabound view that takes only a two part name (dbo.objectname).
> However, a view created on a linked server has four parts in its name
> (linkedserver_name,db_name,owner_nameobj
ect_name). This causes the sql
> parser to reject my request to create a schema bound linked server
> view. Can anybody tell me if there is a work around for this?
> Thanks
> /Sudha
>

Creating schemabound views on linked servers

Hi,
I need to create indexes and use index hints on views created
across two servers (in the linked server model). Assuming that it is
possible to do so, I first need to create an unique clustered index on
the view. This in turn mandates that the underlying view is a
schemabound view that takes only a two part name (dbo.objectname).
However, a view created on a linked server has four parts in its name
(linkedserver_name,db_name,owner_nameobject_name). This causes the sql
parser to reject my request to create a schema bound linked server
view. Can anybody tell me if there is a work around for this?
Thanks
/SudhaAn indexed view cannot store data from a table that resides in another database or another server...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<srajagop@.genesyslab.com> wrote in message
news:1163034549.484430.255030@.i42g2000cwa.googlegroups.com...
> Hi,
> I need to create indexes and use index hints on views created
> across two servers (in the linked server model). Assuming that it is
> possible to do so, I first need to create an unique clustered index on
> the view. This in turn mandates that the underlying view is a
> schemabound view that takes only a two part name (dbo.objectname).
> However, a view created on a linked server has four parts in its name
> (linkedserver_name,db_name,owner_nameobject_name). This causes the sql
> parser to reject my request to create a schema bound linked server
> view. Can anybody tell me if there is a work around for this?
> Thanks
> /Sudha
>

Thursday, March 8, 2012

creating report model timing out

i am trying to create a report model using our sms 2003 sql 2000 database
some of the tables in the model are large and are timing out.
where is the setting that controls the time out parameter in report services
thanksI didn't think you could make report models in SQL 2000, I thought this was
new to SQL 2005. Anyway I had a timeout problem that went away with SQL
2005 sp1. I think sp1 fixed a lot of issues with the ad-hoc report builder
too.
Steve MunLeeuw
"pghboemike" <pghboemike@.discussions.microsoft.com> wrote in message
news:27CA16A8-EFB2-477E-8549-BD92C35ED593@.microsoft.com...
>i am trying to create a report model using our sms 2003 sql 2000 database
> some of the tables in the model are large and are timing out.
> where is the setting that controls the time out parameter in report
> services
> thanks|||Alternatively, If you go to
Start -> All Programs -> Ms SQL Server 2005 -> Configuration Tools ->
Reporting Services Configuration
You can alter the timeout value in there.
Taz
"Steve MunLeeuw" <smunson@.clearwire.net> wrote in message
news:%23Kncke10GHA.1040@.TK2MSFTNGP06.phx.gbl...
>I didn't think you could make report models in SQL 2000, I thought this was
>new to SQL 2005. Anyway I had a timeout problem that went away with SQL
>2005 sp1. I think sp1 fixed a lot of issues with the ad-hoc report builder
>too.
> Steve MunLeeuw|||Modifying the timeout doesn't stop it from timing out. The max in 600
seconds and that isn't long enough for large sets of data. These report
models are pretty much useless unless you have trivial amounts of data.
Pointless...
"Tarun Mistry" wrote:
> Alternatively, If you go to
> Start -> All Programs -> Ms SQL Server 2005 -> Configuration Tools ->
> Reporting Services Configuration
> You can alter the timeout value in there.
> Taz
> "Steve MunLeeuw" <smunson@.clearwire.net> wrote in message
> news:%23Kncke10GHA.1040@.TK2MSFTNGP06.phx.gbl...
> >I didn't think you could make report models in SQL 2000, I thought this was
> >new to SQL 2005. Anyway I had a timeout problem that went away with SQL
> >2005 sp1. I think sp1 fixed a lot of issues with the ad-hoc report builder
> >too.
> >
> > Steve MunLeeuw
>
>

Creating referential integrity constraints

Two ways to do this... Creating the constraints when creating the data model OR using SQL to use the 'reference' constraint. Does 2005 provide any other automated method of creating the Primary Key - to Foreign key constraints without writing the SQL to do this?

Thx

What do you mean by automated method? Using DDL is not automated enough? You could use SMO to create the constraints but you have to write VB.NET or C# code. You can also use the GUI but I wouldn't recommend that because it can mess up your tables depending on what you are doing. For example, it can drop and recreate tables depending on the changes you made in the diagram designer. And this may or may not be the right thing to do. Often using SQL DDL is the most direct and optimized way to make schema changes.

Another option is to use a modelling tool like Visio or ErWin or Power Designer. Those have capabilities to generate scripts to create the schema and/or make changes. Note that the features supported by the tools may differ from the database engine. For example, using included columns or indexing computed columns will be a problem. Similarly, the tools might allow defining cascading foreign key relationships even if there are cycles for example but if you try to apply those changes in SQL Server it will fail because the database engine lacks supports for those. On the other hand, if you are trying the same against Oracle it will succeed. So if you have complex schema relationships then you will have to use a hybrid approach. Or if you support different database engines you will have to tailor the scripts to the specific engine. Visio for example allows you to define additional scripts in the model that can be executed againt the database engine.

|||

Thank you for your answer, I didn't know that the GUI would do the things that you described. That was what I was referring to. I guess for me, the automated method would be that when you create a primary key, the create the foreign key in the 'create table' GUI, the foreign key constraint would be automatically created if the column names are the same. Does this make sense? Maybe coffee is the answer.

As you mentioned above, there are products that do provide this automation, I just wondered if the 2005 engine had included this feature.

Thx

|||

katgreen777 wrote:

the create the foreign key in the 'create table' GUI, the foreign key constraint would be automatically created if the column names are the same. Does this make sense?

It makes sense to some extent. But you can't just go by column names. You could have "Description" column in multiple tables which have no relation to each other except for the fact that they represent some sort of description of an entity. This rule might however work for key attributes.

I don't know if SSMS diagram designer or VS designer infers these things from the model. You have to ask in the Tools or Visual Studio forum. Data modelling tools however does this for you depending on how you create the model. For example, if you drag and drop a key column from one table to another it will establish a relationship that will translate to referential constraint on the database. Of course, none of this comes free. You typically pay a lot for designer/modelling tools.

|||

One note on this... isn't it a rule of thumb not to include computed columns in a table?

thx

|||I don't know of any such rule. There are cases where using computed column is useful. But it is a proprietary feature anyway so for data modelling purpose probably you shouldn't worry about it. If you hit some design issues in SQL Server due to limitations you can use computed columns & indexes on computed columns.|||

The books I've been reading say that you shouldn't include computed columns in a table because of the extra space they take up, but instead, create a view where the computations take place. That was what I was referring to.

Kat

|||Wow! Which books are those? You need to throw them in the garbage. Computed column as the name implies is computed at run-time. There are ways to persist computed column values in SQL Server 2005 which will take up space or if you index computed columns. Otherwise, it is no different than defining a view with the same expression. Even with view the notion that computations take place within a view is incorrect. The view definition is parsed into the statement (SELECT or DML), compiled, optimized and executed.|||

Yes, you aren't the first person to say that about my books, any good ones you would recommend?

Kat

Creating RDL using information contained in an existing Data Model

This question is in regard to creating RDL programmatically, using information contained in an existing Data Model; basically mimicking what the Report Builder does.

What I need to do is integrate a dummied down version of the Report Builder into an existing web application; where users will have the option to build and save reports using a very simple web interface, 4 step process. This process will only have a very few of the options that the Report Builder has.

So far I have built this process referencing the Microsoft Reporting Services dll's (Microsoft.ReportingServices.Modeling.dll for example), in order to retrieve the entity and attributes information from the Data Model.

** Does anyone know how I could create the RDL, using both the entity and attribute information contained in the Data Model in association with selected fields, filters, etc, that is contained in a database?

I can't find anything about this anywhere on the net; any help would be greatly appreciated!

Thanks,

Dan

There isn't an MS tool to automate this with RS 2005. To meet a similar requirement and speed up the implementation effort, we decided to develop an object wrapper on top of RDL which knows how to serialize itself to RDL using the .NET XmlSerializer. In comparison with the XML DOM alternative, we found this approach to work much better for us. You need to familiriaze yourself with the RDL specification beforehand.|||

Btw, more information about the RDL specification mentioned by Teo is available here: http://www.microsoft.com/sql/technologies/reporting/rdlspec.mspx

-- Robert

|||Well thank you very much for this information, I just wanted to make sure I was not missing out on existing functionality!