Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, March 21, 2012

create sequence

is there any function in tsql similar to sqls function create sequence?

Luis:

If you can logically order your records you ought to be able to apply the ROW_NUMBER() OVER( ORDER BY the field))

For instance, if you have a table "A" that has an integer record key "aKey" you can generate sequence numbers something like this:

Code Snippet

select aKey,

row_number() over(order by aKey)

as SEQ

from A

|||

Kent post is applicable to SQL Server 2005 . if its sql 2000, you can try Identity Column or identity function which is also applicable in 2005 . Read about these in BOL (books online)

From BOL

This example inserts all rows from the employee table from the pubs database into a new table called employees. The IDENTITY function is used to start identification numbers at 100 instead of 1 in the employees table.

USE pubs IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'employees') DROP TABLE employees GO EXEC sp_dboption 'pubs', 'select into/bulkcopy', 'true' SELECT emp_id AS emp_num, fname AS first, minit AS middle, lname AS last, IDENTITY(smallint, 100, 1) AS job_num, job_lvl AS job_level, pub_id, hire_date INTO employees FROM employee M adhu|||-edited--

I have this schema

Code Snippet

CREATE Table particle (
idap INT not null,
id INT,
eventid INT not null,
Px Real,
Py Real,
Pz Real,
Kf Real,
Ee Real);

GO

ALTER TABLE particle
ADD CONSTRAINT pk_particle PRIMARY KEY (idap);

GO
Create Table Leptonaux(
id INT not null);

GO

ALTER TABLE leptonaux
ADD CONSTRAINT pk_leptonaux PRIMARY KEY (id);

GO

Alter table leptonaux
ADD Constraint leptonId FOREIGN KEY (id)
REFERENCES particle (idap) ON DELETE CASCADE;

GO

Create Table Muonaux(id INT not null);

GO

ALTER TABLE Muonaux
ADD CONSTRAINT pk_Muonaux PRIMARY KEY (id);

GO

Alter table Muonaux
ADD Constraint muonId FOREIGN KEY (id)
REFERENCES particle (idap) ON DELETE CASCADE;

GO

Create Table electronaux(
id INT not null);

GO

ALTER TABLE electronaux
ADD CONSTRAINT pk_electronaux PRIMARY KEY (id);

GO

Alter table electronaux
ADD Constraint electronId FOREIGN KEY (id)
REFERENCES particle (idap) ON DELETE CASCADE;

GO

Create Table jetaux(
id INT not null);

GO

ALTER TABLE jetaux
ADD CONSTRAINT pk_jetaux PRIMARY KEY (id);

GO

Alter table jetaux
ADD Constraint jetId FOREIGN KEY (id)
REFERENCES particle (idap) ON DELETE CASCADE;

GO


the problem is I filling using a jdbc from another application moving the data from a foreign database.

so, in the the foreign database I make a select * from jetb to move the data to this one and i call it in this way, the proble is idap is an artificial id that doesnt exist in the original schema, so thas why i need the sequence, and then I need a way to get the next value of the sequencia to make a Insert into particle and then an Insert into jetaux with the same sequence number, and the same process with muonaux and electronaux

|||manually i do it in this way

Code Snippet

Insert into particle (idap,Id,eventid,Kf,Px,Py, Pz, Ee)
VALUES(9,2,24455,11,-0.227242,-10.493,-28.8285,30.6796);

Insert into leptonaux(Id)
VALUES(9);

Insert into electronaux (Id)
VALUES(9);

but in i need to create a for each function that use the sequence and I know that in Mimer SQL that is posible, but i dont know how to do it here

in amossql it have to be something like this way

Code Snippet

for each electron e
sql(
Insert into particle (idap,Id,eventid,Kf,Px,Py, Pz, Ee)
VALUES(next value of sequence,id(e),id(event(e)),kf(e),px(e),py(e),pz(e),ee(e));

Insert into leptonaux(Id)
VALUES(same value of sequence);

Insert into electronaux (Id)
VALUES(seme value of sequence);)
);

note that inside sql() function is a string the tsql query (im avoiding some needed code just to make it easy to understand).

next value of sequenca and same value of sequence are the fields that i need to know how to implement
|||

Code Snippet

CREATE Table particle (
idap INT IDENTITY(1,1) not null,
id INT,
eventid INT not null,
Px Real,
Py Real,
Pz Real,
Kf Real,
Ee Real);

Insert your data into all the fields of the particle table except idap.

(ie. insert into particle values(1, 101, 123, 456, 789, 543, 210))

idap will automatically create a sequential counter.

Then when all the data is in the particle table, copy the idap values into your other tables.

Code Snippet

INSERT INTO leptonaux (id)

select distinct idap from particle

repeat for the other tables.

|||

How do you think the solution by IDENTITY() and SCOPE_IDENTITY(), like below.

Code Snippet

CREATE TABLE particle

(

idap int IDENTITY(1,1)

, ....

)

and

Code Snippet

for each electron e

sql

(

Insert into particle (idap,Id,eventid,Kf,Px,Py, Pz, Ee)
VALUES(next value of sequence,id(e),id(event(e)),kf(e),px(e),py(e),pz(e),ee(e));

Insert into leptonaux(Id) VALUES(SCOPE_IDENTITY());

Insert into electronaux (Id) VALUES(SCOPE_IDENTITY());

)


|||the daleJ is the most close solution for me, but the problem rirght now is that for example 1,2,3,4 are muon, 5,6,7 are electron and 8,9,10 are jet for example, but all the particles have to be of one and only one type
|||THX Yosihiro, Im gonna try that one
|||

I don't quite follow that.

Can you clarify a little more?

|||

for some reason its not working, but could be a problem with JDBC, but do I need to put something in next value of sequence?

Code Snippet

for each electron e

sql

(

Insert into particle (idap,Id,eventid,Kf,Px,Py, Pz, Ee)
VALUES(next value of sequence,id(e),id(event(e)),kf(e),px(e),py(e),pz(e),ee(e));

Insert into leptonaux(Id) VALUES(SCOPE_IDENTITY());

Insert into electronaux (Id) VALUES(SCOPE_IDENTITY());

)

|||it was one sintax mistake, now its working.. thanks for the help

Monday, March 19, 2012

Create Report with Credentials

I have a report with a data source that requires credentials to be stored in
reporting service system.
I use the CreateReport function to deploy this report but it doesn't store
the credentials and these credentials are requested when it is about to
render.
How to store these credentials in order to avoid them to be asked during
rendering'
ThanksIn our case, we use script based on the scripting samples (in the RS install
folder), but link the reports to a shared data source created with the
credentials specfied:
Public Sub CreateSQLDataSource(ByVal serverName as String, ByVal
databaseName as String, ByVal userName as String, ByVal password as String)
Dim name As String = databaseName
Dim parent As String = "/"
'Define the data source definition.
Dim definition As New DataSourceDefinition()
definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
definition.ConnectString = "data source=" & serverName & ";initial
catalog=" & databaseName
definition.Enabled = True
definition.EnabledSpecified = True
definition.Extension = "SQL"
definition.ImpersonateUser = False
definition.ImpersonateUserSpecified = True
definition.Username = userName
definition.Password = password
'Use the default prompt string.
definition.Prompt = Nothing
definition.WindowsCredentials = False
Try
rs.CreateDataSource(name, parent, False, definition, Nothing)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Cheers,
'(' Jeff A. Stucker
\
Business Intelligence
www.criadvantage.com
---
"Ale Dori" <AleDori@.discussions.microsoft.com> wrote in message
news:B8D09C36-79B2-4027-A6B5-E49DE21B460B@.microsoft.com...
>I have a report with a data source that requires credentials to be stored
>in
> reporting service system.
> I use the CreateReport function to deploy this report but it doesn't store
> the credentials and these credentials are requested when it is about to
> render.
> How to store these credentials in order to avoid them to be asked during
> rendering'
> Thanks
>|||Is There a way to not create a shared data source?
I would the result obtained by the Deploy of Visual Studio.
"Jeff A. Stucker" wrote:
> In our case, we use script based on the scripting samples (in the RS install
> folder), but link the reports to a shared data source created with the
> credentials specfied:
> Public Sub CreateSQLDataSource(ByVal serverName as String, ByVal
> databaseName as String, ByVal userName as String, ByVal password as String)
> Dim name As String = databaseName
> Dim parent As String = "/"
> 'Define the data source definition.
> Dim definition As New DataSourceDefinition()
> definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
> definition.ConnectString = "data source=" & serverName & ";initial
> catalog=" & databaseName
> definition.Enabled = True
> definition.EnabledSpecified = True
> definition.Extension = "SQL"
> definition.ImpersonateUser = False
> definition.ImpersonateUserSpecified = True
> definition.Username = userName
> definition.Password = password
> 'Use the default prompt string.
> definition.Prompt = Nothing
> definition.WindowsCredentials = False
> Try
> rs.CreateDataSource(name, parent, False, definition, Nothing)
> Catch e As Exception
> Console.WriteLine(e.Message)
> End Try
> End Sub
>
> --
> Cheers,
> '(' Jeff A. Stucker
> \
> Business Intelligence
> www.criadvantage.com
> ---
> "Ale Dori" <AleDori@.discussions.microsoft.com> wrote in message
> news:B8D09C36-79B2-4027-A6B5-E49DE21B460B@.microsoft.com...
> >I have a report with a data source that requires credentials to be stored
> >in
> > reporting service system.
> >
> > I use the CreateReport function to deploy this report but it doesn't store
> > the credentials and these credentials are requested when it is about to
> > render.
> >
> > How to store these credentials in order to avoid them to be asked during
> > rendering'
> >
> > Thanks
> >
>
>|||Look in RS Books Online for GetReportDataSources and DataSource.Item
property
Hopefully that points you in the right direction.
--
Cheers,
'(' Jeff A. Stucker
\
Business Intelligence
www.criadvantage.com
---
"Ale Dori" <AleDori@.discussions.microsoft.com> wrote in message
news:8D611134-94B3-4937-9372-61A00D6F6573@.microsoft.com...
> Is There a way to not create a shared data source?
> I would the result obtained by the Deploy of Visual Studio.
> "Jeff A. Stucker" wrote:
>> In our case, we use script based on the scripting samples (in the RS
>> install
>> folder), but link the reports to a shared data source created with the
>> credentials specfied:
>> Public Sub CreateSQLDataSource(ByVal serverName as String, ByVal
>> databaseName as String, ByVal userName as String, ByVal password as
>> String)
>> Dim name As String = databaseName
>> Dim parent As String = "/"
>> 'Define the data source definition.
>> Dim definition As New DataSourceDefinition()
>> definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
>> definition.ConnectString = "data source=" & serverName & ";initial
>> catalog=" & databaseName
>> definition.Enabled = True
>> definition.EnabledSpecified = True
>> definition.Extension = "SQL"
>> definition.ImpersonateUser = False
>> definition.ImpersonateUserSpecified = True
>> definition.Username = userName
>> definition.Password = password
>> 'Use the default prompt string.
>> definition.Prompt = Nothing
>> definition.WindowsCredentials = False
>> Try
>> rs.CreateDataSource(name, parent, False, definition, Nothing)
>> Catch e As Exception
>> Console.WriteLine(e.Message)
>> End Try
>> End Sub
>>
>> --
>> Cheers,
>> '(' Jeff A. Stucker
>> \
>> Business Intelligence
>> www.criadvantage.com
>> ---
>> "Ale Dori" <AleDori@.discussions.microsoft.com> wrote in message
>> news:B8D09C36-79B2-4027-A6B5-E49DE21B460B@.microsoft.com...
>> >I have a report with a data source that requires credentials to be
>> >stored
>> >in
>> > reporting service system.
>> >
>> > I use the CreateReport function to deploy this report but it doesn't
>> > store
>> > the credentials and these credentials are requested when it is about to
>> > render.
>> >
>> > How to store these credentials in order to avoid them to be asked
>> > during
>> > rendering'
>> >
>> > Thanks
>> >
>>

Create Procedure Output ntext

Hello,

I need to produce with T-SQL a user defined function or stored
procedure that make one SLQ-Statement and prepare as string from the
result set.
The request muss be able to return a very long unicode string. The
return value nvarchar is being truncated so I'm trying to create a
stored procedure, that returns a ntext string.

I can't manage it (I am no T-SQL specialist). Maybe someone can help
me?

Thanks for your help.

--
Here is my sp:
alter procedure F_FUNCTION (@.userid int, @.parentid int, @.status int,
@.return ntext output)
AS
BEGIN
DECLARE @.onelevel nvarchar(4000)
DECLARE @.pos varchar(1000)
DECLARE @.leveldone varchar(100)
DECLARE @.levelplaned varchar(100)
DECLARE @.planeddate nvarchar(4000)
DECLARE @.elementid varchar(10)
DECLARE @.levelid varchar(10)
DECLARE @.levelstatus varchar(10)
DECLARE @.levelupd nvarchar(4000)
DECLARE @.levelauthor varchar(10)
DECLARE @.prevelementid varchar(10)

BEGIN
declare level_cursor CURSOR FOR
SELECT
B.ElementPos,B.LevelID,A.LevelDone,A.LevelPlaned,A .PlanedDate,A.MatrixContentID,A.Status,
convert(varchar,A.Upd,126) as Upd,A.Author
FROM T_TABLE1 as B left outer join T_TABLE2 as A on
(A.MatrixContentID=B.ID AND A.UserID=@.userid AND A.Status<>3)
where B.ParentID=@.parentid
ORDER BY B.ElementID,B.ElementPos
END

set @.onelevel=''

OPEN level_cursor

FETCH NEXT FROM level_cursor
INTO @.pos,@.levelid,@.leveldone, @.levelplaned,
@.planeddate,@.elementid, @.levelstatus,@.levelupd,@.levelauthor

WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.prevelementid=@.elementid

if (@.pos IS NULL)
set @.onelevel=''
else
set @.onelevel=@.pos

if (@.elementid IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.elementid

if (@.levelid IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.levelid

if (@.leveldone IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.leveldone

if (@.levelplaned IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.levelplaned

if (@.planeddate IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.planeddate

if (@.levelstatus IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.levelstatus

if (@.levelupd IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.levelupd

if (@.levelauthor IS NULL)
set @.onelevel=@.onelevel+'*-*'
else
set @.onelevel=@.onelevel+'*-*'+@.levelauthor

-- Part Output
print @.onelevel

if (@.return is NULL)
exec(@.return+@.onelevel)
else
exec(@.return+'*;*'+@.onelevel)

FETCH NEXT FROM level_cursor
INTO @.pos,@.levelid, @.leveldone, @.levelplaned,
@.planeddate,@.elementid,
@.levelstatus,@.levelupd,@.levelauthor

if (@.prevelementid IS NOT NULL AND @.prevelementid=@.elementid)
FETCH NEXT FROM level_cursor
INTO @.pos,@.levelid, @.leveldone, @.levelplaned,
@.planeddate,@.elementid,
@.levelstatus,@.levelupd,@.levelauthor

END

CLOSE level_cursor
DEALLOCATE level_cursor

RETURN

END

--
Call of the function with:
exec dbo.F_FUNCTION 550,1632, 0, ''

Here the beginning of the query analyser output:

1.00000*-*691*-*1684*-*3*-*0*-**-*0*-*2005-09-22T00:43:00*-*277
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '*'.

--I think you cannot return that using an output parameter - you'll have to
return it as a field in a SELECT.

On 24 Oct 2005 08:17:01 -0700, rey@.infoman.de wrote:

>Hello,
>I need to produce with T-SQL a user defined function or stored
>procedure that make one SLQ-Statement and prepare as string from the
>result set.
>The request muss be able to return a very long unicode string. The
>return value nvarchar is being truncated so I'm trying to create a
>stored procedure, that returns a ntext string.
>I can't manage it (I am no T-SQL specialist). Maybe someone can help
>me?
>Thanks for your help.
>--
>Here is my sp:
>alter procedure F_FUNCTION (@.userid int, @.parentid int, @.status int,
>@.return ntext output)
>AS
>BEGIN
> DECLARE @.onelevel nvarchar(4000)
> DECLARE @.pos varchar(1000)
> DECLARE @.leveldone varchar(100)
> DECLARE @.levelplaned varchar(100)
> DECLARE @.planeddate nvarchar(4000)
> DECLARE @.elementid varchar(10)
> DECLARE @.levelid varchar(10)
> DECLARE @.levelstatus varchar(10)
> DECLARE @.levelupd nvarchar(4000)
> DECLARE @.levelauthor varchar(10)
> DECLARE @.prevelementid varchar(10)
> BEGIN
> declare level_cursor CURSOR FOR
> SELECT
>B.ElementPos,B.LevelID,A.LevelDone,A.LevelPlaned,A .PlanedDate,A.MatrixContentID,A.Status,
>convert(varchar,A.Upd,126) as Upd,A.Author
> FROM T_TABLE1 as B left outer join T_TABLE2 as A on
>(A.MatrixContentID=B.ID AND A.UserID=@.userid AND A.Status<>3)
> where B.ParentID=@.parentid
> ORDER BY B.ElementID,B.ElementPos
> END
> set @.onelevel=''
> OPEN level_cursor
> FETCH NEXT FROM level_cursor
> INTO @.pos,@.levelid,@.leveldone, @.levelplaned,
> @.planeddate,@.elementid, @.levelstatus,@.levelupd,@.levelauthor
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
>set @.prevelementid=@.elementid
> if (@.pos IS NULL)
> set @.onelevel=''
> else
> set @.onelevel=@.pos
> if (@.elementid IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.elementid
> if (@.levelid IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.levelid
> if (@.leveldone IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.leveldone
> if (@.levelplaned IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.levelplaned
> if (@.planeddate IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.planeddate
> if (@.levelstatus IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.levelstatus
> if (@.levelupd IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.levelupd
> if (@.levelauthor IS NULL)
> set @.onelevel=@.onelevel+'*-*'
> else
> set @.onelevel=@.onelevel+'*-*'+@.levelauthor
> -- Part Output
> print @.onelevel
> if (@.return is NULL)
> exec(@.return+@.onelevel)
> else
> exec(@.return+'*;*'+@.onelevel)
> FETCH NEXT FROM level_cursor
> INTO @.pos,@.levelid, @.leveldone, @.levelplaned,
>@.planeddate,@.elementid,
> @.levelstatus,@.levelupd,@.levelauthor
> if (@.prevelementid IS NOT NULL AND @.prevelementid=@.elementid)
> FETCH NEXT FROM level_cursor
> INTO @.pos,@.levelid, @.leveldone, @.levelplaned,
>@.planeddate,@.elementid,
> @.levelstatus,@.levelupd,@.levelauthor
> END
> CLOSE level_cursor
> DEALLOCATE level_cursor
> RETURN
>END
>--
>Call of the function with:
>exec dbo.F_FUNCTION 550,1632, 0, ''
>Here the beginning of the query analyser output:
>1.00000*-*691*-*1684*-*3*-*0*-**-*0*-*2005-09-22T00:43:00*-*277
>Server: Msg 170, Level 15, State 1, Line 1
>Line 1: Incorrect syntax near '*'.
>--|||Am 24 Oct 2005 08:17:01 -0700 schrieb rey@.infoman.de:

...
> --
> Call of the function with:
> exec dbo.F_FUNCTION 550,1632, 0, ''
> Here the beginning of the query analyser output:
> 1.00000*-*691*-*1684*-*3*-*0*-**-*0*-*2005-09-22T00:43:00*-*277
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near '*'.
> --

ntext is a valid datatype for the OUTPUT variable of a stored procedure.
This is not the problem. Your error appears here:
...
-- Part Output
print @.onelevel

if (@.return is NULL)
exec(@.return+@.onelevel)
else
exec(@.return+'*;*'+@.onelevel)
...
because if @.return is Null then you do a
exec('1.00000*-*691*-*1684*-*3*-*0*-**-*0*-*2005-09-22T00:43:00*-*277')
and what should this be?
EXEC starts another stored proc, from there you get your error message. And
so it is an error in line 1.

bye
Helmut|||Hello Helmut,

thanks for your answer.
With exec(@.return+@.onelevel) I try to fill my @.return variable with
the content of @.onelevel. I can't do it with set @.return=@.onelevel
because @.return is of type ntext.

How can I do it else?

thanks and bye,
Agns.|||Not sure if you can do this with ntext, but try this

select @.return = @.return + '*;*' + @.onelevel|||(rey@.infoman.de) writes:
> thanks for your answer.
> With exec(@.return+@.onelevel) I try to fill my @.return variable with
> the content of @.onelevel. I can't do it with set @.return=@.onelevel
> because @.return is of type ntext.
> How can I do it else?

You can't. Since you are in a dead end, I suggest that you explain
your underlying business problem that you are trying to solve. What
does the calling side of this look like?

I can offer one workaround: move to SQL 2005, which offers the
new datatype nvarchar(MAX), which in difference to ntext is a first
class citizen.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||On 25 Oct 2005 00:35:55 -0700, rey@.infoman.de wrote:

>Hello Helmut,
>thanks for your answer.
>With exec(@.return+@.onelevel) I try to fill my @.return variable with
>the content of @.onelevel. I can't do it with set @.return=@.onelevel
>because @.return is of type ntext.
>How can I do it else?
>thanks and bye,
>Agns.

What's wrong with returning it via SELECT rather than via a parameter?

Thursday, March 8, 2012

Create non-aggregation (Atomic) measure ?

Hi, All,

There are two dimensions and one measure group. In the measure group, I have only one measure which has no aggregation function on it. What I want to get is list all the measure detail based on the dimension attributes.

I have created this cube in SSAS 2005 successfully, but when I browsed it in browser,

I got error " The query could not be processed. Internal error: an unexpected error occured (file 'mdaggeng.cpp', line 515, function
'MDAggEngine::ComputerValueOrEvaluatot')"

Any idea on this?

Thanks

I'm not sure what is causing this error, but the primary value of SSAS is it's aggregations.

If you truely have one measure group, with one measure and don't want and aggregates done, then you would get better performance out of a SQL table.

|||

Thanks Darren for the advice.

The situation is that we have two cubes which are in different granularity but connected by "Conformed" Dimension. What required to do is to browse these cubes to retrieve the "atomic" (Non Aggregation) measures with the dimension attributes into a data set. And from the dataset, the reporting process will do the calculations. You are right, we can implement this directly against the SQL tables. But we are requested to show business users with "Cube-like" interface. I have created a cube to combine the two cubes in different granularity, but I have the difficult to bring up the measures. All the measures don't show up. In other words, the measures look like empty.

Do I miss something here?

Wednesday, March 7, 2012

create local .CUB File in AS2005

Hi,

we have a problem with the "CREATE GLOBAL CUBE" function in AS2005:

While accessing the local .CUB file (created with "CREATE GLOBAL CUBE"), our application only shows key-values for some dimensions instead of their names.
If we use the same statement in AS2000 to create the .CUB, everything is displayed correctly;
as well if the application directly connects to the DB (AS2000 & AS2005).

We used the Profiler to take a look at the traces and found something in the SELECT that is used to fill the new cube after creation:
every dimension is bracketed with "KEY()"
We tested the statement and replaced the "KEY()" with "NAME()" and it looks like this would bring the correct data.

But how do we tell the AS2005 to use NAME() instead of KEY() while compiling the "CREATE GLOBAL CUBE" statement?

Or is there annother way to create the local .CUB file?
We are able to create a cube via xmla ... but how do we write it to a local file?

Thanks for your answers and for tolerating my bad english
best regards,
Sven

I don't know what you mean by the statement "Our application only shows key-values". If you mean your application only displays dimension member keys instead of dimension member names, then, this is a bug that is being fixed in the SP2 release.

You can capture the local cube statement, modify it suitably and send it via MDX Client.

create local .CUB File in AS2005

Hi,

we have a problem with the "CREATE GLOBAL CUBE" function in AS2005:

While accessing the local .CUB file (created with "CREATE GLOBAL CUBE"), our application only shows key-values for some dimensions instead of their names.
If we use the same statement in AS2000 to create the .CUB, everything is displayed correctly;
as well if the application directly connects to the DB (AS2000 & AS2005).

We used the Profiler to take a look at the traces and found something in the SELECT that is used to fill the new cube after creation:
every dimension is bracketed with "KEY()"
We tested the statement and replaced the "KEY()" with "NAME()" and it looks like this would bring the correct data.

But how do we tell the AS2005 to use NAME() instead of KEY() while compiling the "CREATE GLOBAL CUBE" statement?

Or is there annother way to create the local .CUB file?
We are able to create a cube via xmla ... but how do we write it to a local file?

Thanks for your answers and for tolerating my bad english
best regards,
Sven

I don't know what you mean by the statement "Our application only shows key-values". If you mean your application only displays dimension member keys instead of dimension member names, then, this is a bug that is being fixed in the SP2 release.

You can capture the local cube statement, modify it suitably and send it via MDX Client.

Friday, February 24, 2012

CREATE FUNCTION?

Hello!

How can I make this function in MS SQL:

CREATE FUNCTION id_name() RETURNS INTEGER
AS 'SELECT MAX(ID)+1 FROM Test;'

Thanks!
Big Smile [:D]

CREATE FUNCTION id_name ()
RETURNS integer

AS


BEGIN


declare @.Return integer
@.return =SELECT MAX(ID)+1 FROM Test
return @.return

END

Limno

|||CREATE FUNCTION id_name RETURNS INTEGER
AS SELECT MAX(ID)+1 FROM Test

CREATE FUNCTION owner_name

Hi,

I have a problem with a function I am creating. I am working on a project for Uni. I coded all my SQL on my home computer, but now I have transferred it all to the schools system and I have a problem with the permissions on the schools SQL Server.

On my home computer I created

CREATE FUNCTION GetDateOnly

It automatically gave it the 'dbo' owner_name which worked fine at home, but on the Uni computers it says I dont have permission to execute it. The owner of all my other Procedures, tables etc. is SCMS/ral6 when I create them at school, which is also the owner_name of my function if I create it at school, but...

If I try and run a function like this:

SCMS/ral6.GetDateOnly()

It does not like the '/'

Nor does it work if I leave that part of, and just go:

ral6.GetDateOnly()

Does anyone know how I can run this? The Uni is not likely to change there whole permissions to run my one project, but without this FUNCTION, my project will not work.

Thankstry [SCMS/ral6].GetDateOnly()|||Cheers for that, I think you just saved my life :)

create function

I want to assign the querie to the ptvalue variable... i just want which mistake im making, it trow me an error near to the select.

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
declare @.ptvalue INT
SET @.ptvalue = select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id;
Return @.ptvalue
END

It looks to me like if you enclose your select statement with parends that your query will work.

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
declare @.ptvalue INT
SET @.ptvalue = (select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id);
Return @.ptvalue
END

I will give this a look in a sec for verification.

|||

Yes, the parends will fix the define; you can also define your function without the variable if you choose; something like this:

create function pt
(@.idpt INT)
Returns INT
AS
BEGIN
Return ( select sqrt(a.px*a.px + a.py*a.py)
from abstractparticle as a
where @.idpt = a.id)
END

|||

Another thing to consider is that the fact that this is a scalar function. Referencing this function will likely make your code more readable; however, keep in mind that scalar functions tend to have a negative impact on performance.

If you have queries that use this function that seem to perform badly one way to improve performance at the expense of readability will always be to replace the UDF function references with the SELECT statement that you have enclosed in parends.

create function

I'd like to return a NULL value from a mssql function, but i can't get it
to. here's my sample:
CREATE FUNCTION dbo.FixVarChar (@.val nvarchar(30) )
RETURNS nvarchar(30) AS
BEGIN
declare @.retval as varchar (30)
select @.retval = case when len(@.val)>0 then @.val else null end
return (null)
END
it does NOT return a null value. when I run:
select dbo.fixvarchar('somevalue') as test
it does not return null but what looks like a zero-length string.
i actually need to get the function to return the value (if the string has
len>0) or an actuall NULL value.
help!john wrote on Fri, 9 Jun 2006 10:09:54 -0400:

> I'd like to return a NULL value from a mssql function, but i can't get it
> to. here's my sample:
> CREATE FUNCTION dbo.FixVarChar (@.val nvarchar(30) )
> RETURNS nvarchar(30) AS
> BEGIN
> declare @.retval as varchar (30)
> select @.retval = case when len(@.val)>0 then @.val else null end
> return (null)
> END
> it does NOT return a null value. when I run:
> select dbo.fixvarchar('somevalue') as test
> it does not return null but what looks like a zero-length string.
> i actually need to get the function to return the value (if the string has
>
len>> 0) or an actuall NULL value.
> help!
I've just created your function on my SQL Server 2005 machine, and run the
same select, and get a NULL (when run in Query Analyzer) - and you'll always
get NULL too, unless you fix your last line to be RETURN (@.retval). What
version of SQL Server are you trying this on?
Dan|||Just tried it on SQL Server 2000 too, works fine.
Dan|||it's sql 2000.
I put that last line in there just to ensure i was returning a null value.
(ultimately, the function will return the non-null value if it exists or
null. I have an xml export program that expects null for non-existing
element nodes to be created).
"Daniel Crichton" <msnews@.worldofspack.com> wrote in message
news:uKXChC9iGHA.4344@.TK2MSFTNGP05.phx.gbl...
> Just tried it on SQL Server 2000 too, works fine.
> Dan
>|||I get 'someval' returned on my SQL 2000 SP4 when I replace 'return (null)'
with 'return (@.retval)'. Are you running SP4?
In any case, I see a couple of inconsistencies. You are returning varchar
(30) but the function return data type is nvarchar(30). Also, you are
passing a varchar instead of the nvarchar expected as the function
parameter.
Hope this helps.
Dan Guzman
SQL Server MVP
"john doe" <jdoe@.doe.com> wrote in message
news:%23IjaZ78iGHA.3440@.TK2MSFTNGP02.phx.gbl...
> I'd like to return a NULL value from a mssql function, but i can't get it
> to. here's my sample:
> CREATE FUNCTION dbo.FixVarChar (@.val nvarchar(30) )
> RETURNS nvarchar(30) AS
> BEGIN
> declare @.retval as varchar (30)
> select @.retval = case when len(@.val)>0 then @.val else null end
> return (null)
> END
> it does NOT return a null value. when I run:
> select dbo.fixvarchar('somevalue') as test
> it does not return null but what looks like a zero-length string.
> i actually need to get the function to return the value (if the string has
> len>0) or an actuall NULL value.
> help!
>|||As I said, I tried it on SQL 2000 here, worked fine (returned a null as it
was, returned the value I passed in when I adjusted it to return @.retval,
and a null if the passed in value was a blank string).
Dan
john wrote on Fri, 9 Jun 2006 12:55:24 -0400:
> it's sql 2000.
> I put that last line in there just to ensure i was returning a null value.
> (ultimately, the function will return the non-null value if it exists or
> null. I have an xml export program that expects null for non-existing
> element nodes to be created).
> "Daniel Crichton" <msnews@.worldofspack.com> wrote in message news:uKXChC9i
GHA.4344@.TK2MSFTNGP05.phx.gbl...|||Forgot the ask something else in my reply - how are you testing the return
value? I used query analyser where it shows NULL in the column, but if
you're using something else maybe that is interpreting nulls as empty
strings. Without more information there's not much else I can suggest.
Dan
john wrote on Fri, 9 Jun 2006 12:55:24 -0400:
> it's sql 2000.
> I put that last line in there just to ensure i was returning a null value.
> (ultimately, the function will return the non-null value if it exists or
> null. I have an xml export program that expects null for non-existing
> element nodes to be created).
> "Daniel Crichton" <msnews@.worldofspack.com> wrote in message news:uKXChC9i
GHA.4344@.TK2MSFTNGP05.phx.gbl...

Friday, February 17, 2012

Create date Function using SQL

Do anyone know any functions in SQL to create a new date using numeric numbers eg.
day=1, month=2 , year=2003
final = 01/02/2003
please help thanksto_date(lpad(day,2,'0')||'/'||lpad(month,2,'0')||'/'||year,'dd/mm/yyyy')

Originally posted by Twinki
Do anyone know any functions in SQL to create a new date using numeric numbers eg.

day=1, month=2 , year=2003

final = 01/02/2003

please help thanks