Showing posts with label dbo. Show all posts
Showing posts with label dbo. Show all posts

Tuesday, March 27, 2012

dynamic query problem

I make a sp in mt sql server,

the SP get one parameter, its the WHERE,
and then I execute the query,

here is my SP:
ALTER PROCEDURE [dbo].[rptEventToPsy]
@.strSQL nvarchar(4000)=''
as
DECLARE @.SQLString NVARCHAR(4000);
SET @.SQLString = N'SELECT
dbo.tbl_CA_meeting.userID, dbo.tblUsersName.OrdName, COUNT(dbo.tbl_CA_event.itemInPackageID) AS Expr1, dbo.tbl_CA_itemInPackage.itemInPackageName, dbo.tbl_CA_package.packageName FROM dbo.tbl_CA_meeting INNER JOIN
dbo.tbl_CA_event ON dbo.tbl_CA_meeting.eventID = dbo.tbl_CA_event.eventID
INNER JOIN
dbo.tbl_CA_itemInPackage ON dbo.tbl_CA_event.itemInPackageID = dbo.tbl_CA_itemInPackage.itemInPackageID
INNER JOIN
dbo.tbl_CA_package ON dbo.tbl_CA_itemInPackage.packageID = dbo.tbl_CA_package.packageID
INNER JOIN
dbo.tblUsersName ON dbo.tbl_CA_meeting.userID = dbo.tblUsersName.UserId
INNER JOIN
dbo.tbl_CA_mishmeret ON dbo.tbl_CA_meeting.mishmeretID = dbo.tbl_CA_mishmeret.mishmeretID ';
SET @.SQLString = @.SQLString + @.strSQL ;
SET @.SQLString = @.SQLString + '
GROUP BY
dbo.tbl_CA_meeting.userID,
dbo.tblUsersName.OrdName,
dbo.tbl_CA_itemInPackage.itemInPackageName,
dbo.tbl_CA_package.packageName';
EXEC sp_ExecuteSql @.SQLString
return;

my problem is in the Reports, I cant to connect the data to the fileds, (the value is empty)

how can I do it?First, your stored procedure is very susceptible to SQL injection. You really need to constrain the input more. There are several articles on the web that will help you. You can accomplish the same result by using an expression for the SQL statement in the report and write some functions to check the input values.sql

Monday, March 26, 2012

Dynamic query

I use Dynamic Query:
="Select dbo.UserList.UserID FROM dbo.UserList
WHERE dbo.UserList.IncidentID = " & Parameters!IncidentID.Value
While I built the solution I had:
The expression for the query ?DataSource2? contains an error: [BC30648]
String constants must end with a double quote.
Tried to play with quotes - nothing helps. What can be wrong?
Thank you in advance.I assume that IncidentID is a varchar or something like that. So you need to
enclose your parameter is single quotes. When figuring this out I usually
have a textbox that I set the expression to so I can see the result. What
you want is this:
="Select dbo.UserList.UserID FROM dbo.UserList WHERE dbo.UserList.IncidentID
= '" & Parameters!IncidentID.Value & "'"
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Irina Pippin via SQLMonster.com" <forum@.nospam.SQLMonster.com> wrote in
message news:80756350b0964be882d691763129e7bd@.SQLMonster.com...
>I use Dynamic Query:
> ="Select dbo.UserList.UserID FROM dbo.UserList
> WHERE dbo.UserList.IncidentID = " & Parameters!IncidentID.Value
> While I built the solution I had:
> The expression for the query 'DataSource2' contains an error: [BC30648]
> String constants must end with a double quote.
> Tried to play with quotes - nothing helps. What can be wrong?
> Thank you in advance.|||another solution
=string.Format("Select dbo.UserList.UserID FROM dbo.UserList WHERE
dbo.UserList.IncidentID ='{0}' ", Parameters!IncidentID.Value)
"Irina Pippin via SQLMonster.com" <forum@.nospam.SQLMonster.com> wrote in
message news:80756350b0964be882d691763129e7bd@.SQLMonster.com...
>I use Dynamic Query:
> ="Select dbo.UserList.UserID FROM dbo.UserList
> WHERE dbo.UserList.IncidentID = " & Parameters!IncidentID.Value
> While I built the solution I had:
> The expression for the query ?DataSource2? contains an error: [BC30648]
> String constants must end with a double quote.
> Tried to play with quotes - nothing helps. What can be wrong?
> Thank you in advance.|||Cool, I'd never seen that solution before. I like it, especially if you have
multiple parameters.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Oleg Yevteyev" <myfirstname001atgmaildotcom> wrote in message
news:eRVfDfpUFHA.548@.tk2msftngp13.phx.gbl...
> another solution
> =string.Format("Select dbo.UserList.UserID FROM dbo.UserList WHERE
> dbo.UserList.IncidentID ='{0}' ", Parameters!IncidentID.Value)
> "Irina Pippin via SQLMonster.com" <forum@.nospam.SQLMonster.com> wrote in
> message news:80756350b0964be882d691763129e7bd@.SQLMonster.com...
>>I use Dynamic Query:
>> ="Select dbo.UserList.UserID FROM dbo.UserList
>> WHERE dbo.UserList.IncidentID = " & Parameters!IncidentID.Value
>> While I built the solution I had:
>> The expression for the query 'DataSource2' contains an error: [BC30648]
>> String constants must end with a double quote.
>> Tried to play with quotes - nothing helps. What can be wrong?
>> Thank you in advance.
>

Thursday, March 22, 2012

Dynamic order by and JOIN

Hi,
Here is my code:
ALTER PROCEDURE dbo.sp_GetPeopleDetails_1
@.OrderByClause varchar(100)
AS
DECLARE @.SQLStatement varchar(255)
SELECT @.SQLStatement = 'SELECT People.PeopleID, People.FirstLastName, People.Title,
Departments.AcademicArea, Shifts.ShiftName, People.TShirt,
People.ParkingFROM Departments INNER JOIN
People ONDepartments.DepartmentID = People.DepartmentID
INNER JOINShifts ON People.ShiftID = Shifts.ShiftID
order By ' +@.OrderByClause
EXEC(@.SQLStatement)
/* SET NOCOUNT ON */
RETURN
When I run it, the error is: "Incorrect syntax near the keyword 'IN'."
Can anyone point my mistake?
Thanks.

Could the 'IN' be in the @.OrderByClause text?

Brian

|||Does this happen with any orderbyclause or just a particular one?|||

run the query in the Query analyzer passing the order by clause and do a PRINT(@.SQLStatement) to see how its building up.

On another note you should not be prefixing your stored proc with "SP_". It causes performance degradation as the sql server will first look under master database assuming its a system proc and then not finding there will look under the database you ware working with.

|||

Finally, problem solved, when I run it in Query analyzer itshowed a truncated query, very strange as I have had it set to varchar(255). Ijust increased it to 500 even though the query does not have more 255characters.
Thanks for the tip with the sp_

Friday, March 9, 2012

Dynamic Execute in Function?

Is it possible to dynamically execute SQL in a Function?
Set @.selectit = ' Select Startdate from [dbo].[' + @.tablename
+ '] Where testcol=113'
Exec (@.selectit )
Is what I want to do but Query Analyzer won't let me do it.
SQL Server 2000.
Thanks.On Fri, 30 Sep 2005 16:54:52 -0400, xenophon wrote:

>Is it possible to dynamically execute SQL in a Function?
Hi xenophon,
No, that's not possible. See "User-Defined Functions" in Books Online.
Why would you want to do this anyway? Why don't you know the name of
your table?
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||WHO CARES IF IT POSSIBLE?
No programmer who has has a **basic** software engineering course would
use dynamic SQL in the middle of a procedure. Remember the terms
"coupling " and "cohesion"'|||Hello,
You are unable to use it in Function. However, you can use it in stored
procedure. The sample code for your reference:
create proc aaa (@.tablename varchar(10) )
as
begin
declare @.sec varchar(300)
set @.sec= ' Select customerID from ' + @.tablename + ' Where
customerID="ALFKI"'
exec (@.sec)
end
exec aaa 'customers'
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
========================================
=============
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
=============
This posting is provided "AS IS" with no warranties, and confers no rights.

Wednesday, March 7, 2012

Dynamic Database Owner

I've got a GridView on my webform bound to a SQLDataSource (called sqlZKEWILL). Works fine as long as I hard-code the "dbo" database owner in the Select statement, as in:

select EXIDV, Carton_Count, Carton_Total, dbo.ZINSPECTION where EXIDV = @.CartonID

The problem is that I need the database owner name to be dynamic. This will be a parameter that I read from another SQL table. For example, I'm doing this:

Select USERID, DatabaseOwner from tblUsers where UserID = 'Kimm'

I need to take the DatabaseOwner from this second Select statement, and use it as part of that first select statement.

On the CodeBehind page, I tried replacing "dbo" with the Database Owner field (which I saved in a session variable) with the following code:

PrivateSub gvLineItems_DataBinding(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles gvLineItems.DataBinding

sqlZKEWILL.SelectCommand = sqlZKEWILL.SelectCommand.Replace("dbo", Session("DatabaseOwner").ToString.Trim)

EndSub

But I am still getting the error message: "Invalid object name: dbo.ZINSPECTION". It doesn't seem to recognize that I've replaced "dbo" with a different value.

Any thoughts on getting this to work would be appreciated.

Hi,

From your description, it seems that you want to change the select command in the DataBinding event of your SqlDataSource, right?

Based on my knowledge, when the DataBinding event has fired, the select command has been in "ready for execute" status, so when you modify the select command in that event, it won't affect this time's execution. I suggest that you should modify your select command in some events of data bound control. Such as databinding event of GridView control.


Besides, since you are changing the database owner, please make sure that if the database owner matches the data tables, otherwise, you can't access the those data objects.

Thanks.

|||

Thank you for the advice. The solution I ended up with is similar to your suggestion. I ended up re-binding the GridView every time the user enters values in a text box, to allow me to dynamically change both the database owner and the where claus as needed. I appreciate your taking the time to help.

Sunday, February 19, 2012

dynamic columns

Hi all, a quicky.

What is wrong with:

CREATE PROCEDURE dbo.QuotePrice
@.DateFrom datetime,
@.DateTo datetime
AS
declare @.days as int
declare @.price as smallint
declare @.daycolumn as nvarchar(6)

set @.price = 0

set @.days = DATEDIFF(day, @.DateFrom, @.DateTo)

set @.daycolumn = 'Day_' + CAST(@.days AS nvarchar(2))

set @.price = (SELECT @.daycolumn FROM pasPriceTable)

return @.price
GO

Problem is:
Syntax error converting the nvarchar value 'Day_10' to a column of data type smallint

How do I set a variable to a result from a query?
Thanks,
/ j0rgeYou can't use a variable to reference a column name...
You need to build a string and then eval the string... see below.

BTW, you'll want to put a where clause in to be sure you return a single value for price..

You should look into using sp_executesql as well...

Hope this helps...
jzapp
--------------------
CREATE PROCEDURE dbo.QuotePrice
@.DateFrom datetime,
@.DateTo datetime
AS
declare @.days as int

declare @.daycolumn as nvarchar(6)

declare @.str varchar(500)

set @.days = DATEDIFF(day, @.DateFrom, @.DateTo)

set @.daycolumn = 'Day_' + CAST(@.days AS nvarchar(2))

SET @.str = '
declare @.price as smallint
set @.price = 0
set @.price = (SELECT ' + @.daycolumn + ' FROM pasPriceTable WHERE ID =1)
return @.price
'
EXEC(@.str)

GO|||You can't use a variable to reference a column name...
YES YOU CAN. This works:

CREATE PROCEDURE dbo.pasQuotePrice
@.DateFrom datetime,
@.DateTo datetime
AS
declare @.days as int
declare @.price as smallint
declare @.daycolumn as nvarchar(6)

set @.price = 0

set @.days = DATEDIFF(day, @.DateFrom, @.DateTo)

set @.daycolumn = 'Day_' + CAST(@.days AS nvarchar(2))

SELECT @.daycolumn FROM pasPriceTable
GO

BUT the result gets returned as a record not as a value with return which is what I need.
/ jorge

Originally posted by jzapp
You can't use a variable to reference a column name...
You need to build a string and then eval the string... see below.

BTW, you'll want to put a where clause in to be sure you return a single value for price..

You should look into using sp_executesql as well...

Hope this helps...
jzapp
--------------------
CREATE PROCEDURE dbo.QuotePrice
@.DateFrom datetime,
@.DateTo datetime
AS
declare @.days as int

declare @.daycolumn as nvarchar(6)

declare @.str varchar(500)

set @.days = DATEDIFF(day, @.DateFrom, @.DateTo)

set @.daycolumn = 'Day_' + CAST(@.days AS nvarchar(2))

SET @.str = '
declare @.price as smallint
set @.price = 0
set @.price = (SELECT ' + @.daycolumn + ' FROM pasPriceTable WHERE ID =1)
return @.price
'
EXEC(@.str)

GO|||When you select a variable such as...
SELECT @.colname FROM table, what you get is the actual string value of that variable for every record in the table... and not the data value of the field.
It's equivalent to...
SELECT 'Day_10' FROM table (SQL interprets Day_10 as a string and not as a fieldname)
Your recordset is probably...
Day_10
Day_10
Day_10
.
.
.

Also, as I stated before, unless you know that this query will only return one record, you will need to put in a WHERE clause..

regards...|||Weird, i swar it worked somehow before.
Thanks.
/ jorge

Originally posted by jzapp
When you select a variable such as...
SELECT @.colname FROM table, what you get is the actual string value of that variable for every record in the table... and not the data value of the field.
It's equivalent to...
SELECT 'Day_10' FROM table (SQL interprets Day_10 as a string and not as a fieldname)
Your recordset is probably...
Day_10
Day_10
Day_10
.
.
.

Also, as I stated before, unless you know that this query will only return one record, you will need to put in a WHERE clause..

regards...|||Oh no, trouble running the EXEC version.
A RETURN statement with a return value cannot be used in this context.
/ jorge

Originally posted by j0rge
Weird, i swar it worked somehow before.
Thanks.
/ jorge|||rats...

How about
SELECT @.price AS Price|||Nope.. same thing.

See the problem is I need a return value and returns cannot be a part of a EXEC statement.

BUT if there is a @.@. command to get the last run query then I could:

set @.price = (select price from @.@.lastrunstatementrowone)
return @.price

I've battled with this for a while now.

Maybe there is another way to make the table.
I have a specific amount of days which have one price for a certain period.

that is:
Valid from 12/2003 to 12/2004
Day1=20
Day2=25
...
Day40=240

There can be 'overrides' for specific dates and days.
Valid from 04/2004 to 05/2004
Day20=60
Day21=60

Any ideas? instead of running dynamic SQL?
/ jorge


Originally posted by jzapp
rats...

How about
SELECT @.price AS Price|||Just off the top of my head you could do something like this...
(I haven't tested this code)

SET @.str = '.....
set @.price = 0
set @.price = (SELECT ' + @.daycolumn + ' FROM pasPriceTable WHERE ID =1)

CREATE TABLE #temptable (
ColA [int] IDENTITY (1, 1) NOT NULL,
ColB varchar(50) NOT NULL
)

INSERT INTO #temptable (ColB) VALUES ( @.price )
'

EXEC(@.str)

declare @.p varchar(20)
SELECT @.p = ColB FROM #temptable WHERE ColA = @.@.identity
return @.p|||This code will run A LOT. I want it to be simple, and there must be a way of querying the 'last ran statement'. NO?

Thanks for you help.
/ jorge

Originally posted by jzapp
Just off the top of my head you could do something like this...
(I haven't tested this code)

SET @.str = '.....
set @.price = 0
set @.price = (SELECT ' + @.daycolumn + ' FROM pasPriceTable WHERE ID =1)

CREATE TABLE #temptable (
ColA [int] IDENTITY (1, 1) NOT NULL,
ColB varchar(50) NOT NULL
)

INSERT INTO #temptable (ColB) VALUES ( @.price )
'

EXEC(@.str)

declare @.p varchar(20)
SELECT @.p = ColB FROM #temptable WHERE ColA = @.@.identity
return @.p