|
I figured it out I had to convert to text stream and then use derived column add as new column (DT_STR,4000,1252)(DT_TEXT,1252)[Description]
What a pain!
|
|
|
|
|
Hi guys,
Is there any way to have SQL Server return the table name as part of the column please? In my example, I would like to do something akin to...
SELECT * FROM [Address]
and have SQL Server return a result set containing columns with "Address." prepended to the column name.
Address.ID, Address.Line1, Address.Line2
Many Thanks for any help you can provide!
|
|
|
|
|
Try this
select ID as 'Address.ID', Line1 as 'Address.Line1' ....
But why do you need it? Column names are unique in a select.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hi!
Thank you for your reply.
Unfortunately explicitly naming the columns is the only way I've found to do this so far. I would however like to be able to do something like SELECT * FROM Address and retrieve the columns as [Address].[name].
Do you know of a way of doing this without explicitly aliasing the column names please?
|
|
|
|
|
Sorry, no. I assume your query sometimes joins several tables, otherwise it would be a trivial thing to auto-generate your query.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
That is correct. In some instances I will be selecting fields from a view and would like to simply have
SELECT * FROM vAddress AS Address
which would output
Address.ID, Address.Line1, Address.Line2
Thank you for your help. Looks like I'm gonna have to explicitly state the aliases in my procs.
Cheers
|
|
|
|
|
I dont know why you need to do this but you can just
SELECT 'Address.' + ID AS ID, 'Address.' + Line1 AS Line1
FROM [Address]
If your trying to use this to make it relational on the client side this is a bad way to do it.
|
|
|
|
|
I'm attempting to build a DAL that will automatically create and populate objects from the results of a database call.
As such, I need the data to be returned in column names that represent the object they are populating such as object.property e.g. Address.ID, Address.Line1
Sometimes I need just the property names e.g. ID, Line1, Line2 and other times I need Address.ID, Address.Line1, Address.Line2. It would be nice to have a view that I can call which returns the relevent fields and then I can simply prepend the object name to each of the fields when required.
|
|
|
|
|
I dont know if this is helpful but here is an example of my customer DAL. I use Microsoft patterns and practices for SQL data access but you can just use SQL data adapter.
I just dim cust as new DAL.Customer()
cust.CustomerName = textbox1.text
....
cust.save
or
cust.FetchCustomer(22)
Imports Microsoft.VisualBasic
Namespace DAL
Public NotInheritable Class Customer
#Region " Local Variables "
Private _CustomerId As Integer
Private _CustomerName As String
Private _Address1 As String
Private _Address2 As String
Private _City As String
Private _StateProvince As String
Private _ZipPostal As String
Private _CountryCode As String
Private _DateCreated As Date
Private _DateUpdated As Date
Private _IsDirty As Boolean = False
#End Region
#Region " Public Properties "
Public Property CustomerId() As Integer
Get
Return _CustomerId
End Get
Set(ByVal value As Integer)
If value <> _CustomerId Then
_IsDirty = True
End If
_CustomerId = value
End Set
End Property
Public Property CustomerName() As String
Get
Return _CustomerName
End Get
Set(ByVal value As String)
If value <> _CustomerName Then
_IsDirty = True
End If
_CustomerName = value
End Set
End Property
Public Property Address1() As String
Get
Return _Address1
End Get
Set(ByVal value As String)
If value <> _Address1 Then
_IsDirty = True
End If
_Address1 = value
End Set
End Property
Public Property Address2() As String
Get
Return _Address2
End Get
Set(ByVal value As String)
If value <> _Address2 Then
_IsDirty = True
End If
_Address2 = value
End Set
End Property
Public Property City() As String
Get
Return _City
End Get
Set(ByVal value As String)
If value <> _City Then
_IsDirty = True
End If
_City = value
End Set
End Property
Public Property StateProvince() As String
Get
Return _StateProvince
End Get
Set(ByVal value As String)
If value <> _StateProvince Then
_IsDirty = True
End If
_StateProvince = value
End Set
End Property
Public Property ZipPostal() As String
Get
Return _ZipPostal
End Get
Set(ByVal value As String)
If value <> _ZipPostal Then
_IsDirty = True
End If
_ZipPostal = value
End Set
End Property
Public Property CountryCode() As String
Get
Return _CountryCode
End Get
Set(ByVal value As String)
If value <> _CountryCode Then
_IsDirty = True
End If
_CountryCode = value
End Set
End Property
Public ReadOnly Property DateCreated() As Date
Get
Return _DateCreated
End Get
End Property
Public ReadOnly Property DateUpdated() As Date
Get
Return _DateUpdated
End Get
End Property
#End Region
Public Sub New()
'blank
End Sub
Public Function Save() As Boolean
If _IsDirty Then
Dim cmd As DbCommand
Dim db As Database = DatabaseFactory.CreateDatabase("CustomerDb")
cmd = db.GetStoredProcCommand("sp_Customer_iu")
Try
' add input parameters
db.AddInParameter(cmd, "CustomerID", DbType.Int32, _CustomerId)
db.AddInParameter(cmd, "CustomerName", DbType.String, _CustomerName)
db.AddInParameter(cmd, "CompanyName", DbType.String, _CompanyName)
db.AddInParameter(cmd, "Address1", DbType.String, _Address1)
db.AddInParameter(cmd, "Address2", DbType.String, _Address2)
db.AddInParameter(cmd, "CityName", DbType.String, _City)
db.AddInParameter(cmd, "StateProvince", DbType.String, _StateProvince)
db.AddInParameter(cmd, "ZipPostal", DbType.String, _ZipPostal)
db.AddInParameter(cmd, "CountryCode", DbType.String, _CountryCode)
' Add output paramaters
db.AddOutParameter(cmd, "ReturnCustomerId", DbType.Int32, 4)
db.ExecuteNonQuery(cmd)
_CustomerId = db.GetParameterValue(cmd, "ReturnAccountID")
Return True
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
cmd.Dispose()
End Try
Else
Return True
End If
End Function
Public Function FetchCustomer(ByVal CustomerId As Integer) As Customer
Dim db As Database = DatabaseFactory.CreateDatabase("CustomerDb")
Dim cmd As DbCommand = db.GetStoredProcCommand("sp_Customer_s")
Dim Output As New Customer
db.AddInParameter(cmd, "CustomerId", DbType.Int32, CustomerId)
Dim dr As IDataReader = db.ExecuteReader(cmd)
Try
If dr.Read Then
Output.LoadData(dr)
End If
Finally
dr.Close()
cmd.Connection.Close()
End Try
Return Output
End Function
Friend Sub LoadData(ByVal reader As IDataReader)
Dim i As Integer
With reader
For i = 0 To .FieldCount - 1
If Not .IsDBNull(i) Then
Select Case .GetName(i).ToLower
Case "customerid"
_CustomerId = .GetInt32(i)
Case "accountname"
_CustomerName = .GetString(i)
Case "address1"
_Address1 = .GetString(i)
Case "address2"
_Address2 = .GetString(i)
Case "cityname"
_City = .GetString(i)
Case "stateprovince"
_StateProvince = .GetString(i)
Case "zippostal"
_ZipPostal = .GetString(i)
Case "countrycode"
_CountryCode = .GetString(i)
Case "datecreated"
_DateCreated = .GetDateTime(i)
Case "dateupdated"
_DateUpdated = .GetDateTime(i)
End Select
End If
Next
End With
End Sub
End Class
End Namespace
|
|
|
|
|
Hi,
Thank you for your posting. This is essentially what I am doing with my DAL however am using reflection to make the DAL more generic across objects. As such, the column names in the returned data set must match the property names on the object.
Additionally, my DAL is able to perform an eager-loading of sub-structures. It is for this eager loading that I need to have the table name returned as part of the column name e.g.
Address
-- ID
-- Line1
-- Line2
-- Country
-- -- ID
-- -- Name
-- Postcode
By performing a join between the Address table and Country table, I can eagerly load the country information as a structure into the Address object. I just need to be able to identify the ID and Name fields as belonging the Country structure by prepending 'Country.' to the names.
In the above example, it's fine to explicity alias the field names however I would like a solution which could perform something like...
SELECT
Address.ID AS ID,
Address.Line1 AS Line1,
Address.Line2 AS Line2,
Country.*,
Address.Postcode AS Postcode
FROM
Address LEFT JOIN Country etc...
and which would rename the fields in Country to Country.ID and Country.Name. Obviously I could use a view for this however there may be times when I need the ID and Name property without the 'Country.' prefix. In such instances, it would be nice to reuse the same view instead of having a view that prepends the prefix and a view which does not.
|
|
|
|
|
Hi,
Currently we are shifted on Oracle from MsSQL 2005. I want to convert a stored procedure which will take dynamic query in string format and execute it. Here is MsSQL equivalant:
<code>CREATE Proc [dbo].[GetCars]
@YearOfManf Int,
@PriceFrom Decimal,
@PriceTo Decimal
As
Begin
Declare @sql nVarchar(2000)
set @sql=''
If(@YearOfManf is not null)
set @sql=@sql+' And ManufactureYear='+Convert(Varchar(4),@YearOfManf)+''
If(@PriceFrom Is Not Null and @PriceTo Is Not Null)
set @sql=@sql+' And ExpectedPrice Between '+Convert(Varchar(10),@PriceFrom)+' And '+Convert(Varchar(10),@PriceTo)+''
IF(@PriceFrom Is Not Null and @PriceTo Is Null)
SET @sql=@sql+' And ExpectedPrice &gt;= '+Convert(Varchar(10),@PriceFrom)+''
set @sql='SELECT
CarID,
ManufactureYear
ExpectedPrice
FROM
CarMaster
WHERE Visible = 1 AND Deleted = 0 '
+ @sql +'
GROUP BY
ID, ManufactureYear, ExpectedPrice'
exec sp_executesql @sql
End</code>
Is anybody knw how to convert this line: <b>exec sp_executesql @sql</b>
Thanx in advance...
-Abhijit
|
|
|
|
|
Hi,
In Oracle use either EXECUTE IMMEDIATE statement or DBMS_SQL -package for dynamic sql.
Mika
|
|
|
|
|
hi all,
i use following statement to Get all SQL server instance on my local network
Dim instance As System.Data.Sql.SqlDataSourceEnumerator = System.Data.Sql.SqlDataSourceEnumerator.Instance()<br />
Dim table1 As System.Data.DataTable = instance.GetDataSources()<br />
For Each row As DataRow In table1.Rows<br />
cboServer.Items.Add(row.Item(0).ToString)<br />
Next
Above code Add all the Server in combo box which is in local network. Now i select a Server and try to connect to server as following
Dim MainServer As Server = New Server(cboServer.Text.Trim & "\SQLEXPRESS")<br />
With MainServer.ConnectionContext<br />
.LoginSecure = True<br />
.Connect()<br />
End with
but above line give error "Failed to connect to server MACHINENAME\SQLEXPRESS."
Detail of error(Inner Exception) show "{"Login failed for user 'MACHINENAME\Guest'."}"
MACHINENAME is name of server
I try lot of googling. But nothing is helpful.
Can any body help me that how can i resolve this error or How can i enable the Guest account on Server so it is connected.
|
|
|
|
|
It's obvious to the point where it glares right at you.
The windows account MACHINENAME\Guest does NOT have login privileges for the SQL Server instance. You either have to run the application as a different user or have to give login rights to MACHINENAME\Guest in SQL Server Security permissions.
If you have a SQL Server username and password, use that to connect.
And.
If you're not in a domain, Windows Authentication would not work if you're trying to connect to a SQL Server instance in another machine in the network.
sunil goyalG wrote: How can i enable the Guest account on Server so it is connected.
Ask your administrator to do it. Or if you have access right to that machine and you're an admin, the Guest account can be enabled using the System Management MMC. You'd also need to add Guest as a login in SQL Server. For that, use Management Studio --> Security --> Logins to do that.
|
|
|
|
|
SimulationofSai wrote: You either have to run the application as a different user or have to give login rights to MACHINENAME\Guest in SQL Server Security permissions.
How can i give right to MACHINENAME\Guest in SQL Server Security permissions ?
What action i performed after (Management Studio --> Security --> Logins) ? .WIndows XP is running on both computer(Local and Remote)
|
|
|
|
|
sunil goyalG wrote: What action i performed after
In my opinion, the first action you've got to perform is buy a good book on SQL Server and read it.
|
|
|
|
|
Yaa. I feel this thing . I have no idea, that what is the role of Security (Logins, Server Roles and Credentials). Presently i simply want to test that how to connect to a server so i ask this stupid question.
|
|
|
|
|
sir , I done this thing. Now I want to do this thing in programmatically. Let us explain me
1. Enumerate all the server which is on local Network(This task is already done)
2. Connect to a specific server which is on network .
Task 1 is completed .Now i want to connect to a server. which is on the network.Problem is that how can i connect to Server without login info ? if this thing is not possible then how can i create guest account on selected server so i can connect.
|
|
|
|
|
sunil goyalG wrote: Problem is that how can i connect to Server without login info ?
Depending on configuration you can try trusted connection.
sunil goyalG wrote: how can i create guest account on selected server so i can connect.
You can't. Thats called hacking
Bob
Ashfield Consultants Ltd
|
|
|
|
|
i want to combine like and in operator in sql query...
any one help me.................
modified on Monday, August 4, 2008 7:01 AM
|
|
|
|
|
Whats difficult? Show us your code and someone will point out your error.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
suppose u have a table with employee details..
there is a column address. which consist city name too.
now i want to search employee detail which live in city1 or city2 or city3
now here also a problem ' how many cities user want to search . this is not decide..
now sir , the ball is in your court... face the problem..
good luck...
|
|
|
|
|
Firstly, you asked how to combine like and in - thats not what you are asking really.
As I said, its easy enough. This is the sort of 'problem' faced daily, and there are several ways of doing it.
Anudeep Jaiswal wrote: now sir , the ball is in your court... face the problem..
No, the ball is in YOUR court. Post your attempt and I will point out your errors, but I will not write your code for you - its you being paid to do it, not me.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Anudeep Jaiswal wrote: now sir , the ball is in your court... face the problem..
A little courtesy might go a long way in getting answers to your questions on CP
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|